# 服务端代码
from threading import Thread
import socket
import time
soc = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
soc.bind(('127.0.0.1',8888))
soc.listen(3)
print('wait client...')
def task(conn):
data = conn.recv(1024)
print(f'子线程接收到了数据:{data}')
time.sleep(3)
# 睡了三秒之后服务端将客户端发送来的数据处理后再传给了服务端
conn.send(data.upper())
if name == 'main':
conn,addr = soc.accept()
print(f'client {addr} connect...')
t = Thread(target=task,args=(conn,))
t.start()
print('这是主线程')
客户端:
# 客户端代码
import socket
soc = socket.socket()
soc.connect(('127.0.0.1',8888))
while True:
msg = input('input msg:')
soc.send(msg.encode('utf8'))
data = soc.recv(1024)
print(data)