Socket-Client通信

2018-04-18 17:02:18 浏览数 (1)

新建server.py写入代码如下

代码语言:javascript复制
import socket
HOST="127.0.0.1"
PORT=6666
s=socket.socket()
s.bind((HOST,PORT))  
s.listen(1)
back="ok"
while 1:
	conn,addr=s.accept()  
	print('Connected by' str(addr))
	while 1 :
		data=conn.recv(1024)
		print(str(data))
		conn.sendall(bytes(back, encoding = "utf8"))
conn.close()

新建Client.py

代码语言:javascript复制
import socket
HOST='127.0.0.1'
PORT=6666
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((HOST,PORT))       
while 1:
	cmd=input("Please input cmd:") 
	s.sendall( bytes(cmd, encoding = "utf8")  )     
	data=s.recv(1024)    
	print (str(data))      
s.close()  

0 人点赞