服务端:
import socket
server = socket.socket()
server.bind(('localhost', 6666))
server.listen()
while True:
print('waiting...')
coon,addr = server.accept()
print('coming!',addr)
while True:
rec = coon.recv(1024)
if len(rec)<=0:
print(addr,'lost...')
break
print('rec:',rec)
coon.send(rec.upper())
server.close()
客户端:
import socket
client= socket.socket()
client.connect(('localhost',6666))
while 1:
try:
send = input('>>:')
client.send(bytes(send,encoding = 'utf-8'))
rec = client.recv(1024)
print(rec)
except KeyboardInterrupt:
exit()
client.close()