新建server.py写入代码如下
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
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()