# 服务端
import socket
from threading import Thread
server=socket.socket()
server.bind(
('127.0.0.1',8888)
)
server.listen(5)
def task(conn):
while True:
data=conn.recv(1024)
print(data.decode('utf-8'))
msg=input('>>>>').strip()
conn.send(msg.encode('utf-8'))
while True:
conn, addr = server.accept()
print(addr)
t=Thread(target=task,args=(conn,))
t.start()
# 客户端
import socket
client = socket.socket()
client.connect(
('127.0.0.1', 8888)
)
while True:
try:
msg = input('请输入').encode('utf-8')
client.send(msg)
data = client.recv(1024)
print(data.decode('utf-8'))
except Exception as e:
print(e)
break
client.close()