服务端
from multiprocessing import Process
from threading import Thread
import socket
import time
# TCP 服务端实现并发的效果
server = socket.socket()
server.bind(('127.0.0.1', 8083))
server.listen(5)
# 通信循环
def talk(conn):
while 1:
try:
data = conn.recv(1024)
if len(data) == 0: break
print(data.decode('utf-8'))
conn.send(data.upper())
except Exception as e:
print(e)
break
conn.close()
while 1:
conn, addr = server.accept()
# t = Process(target=talk,args=(conn,))
t = Thread(target=talk, args=(conn,))
t.start()
import socket
import time
# 客户端
# 不输入默认是TCP协议
client = socket.socket()
client.connect(('127.0.0.1', 8083))
while 1:
client.send(b'hello world')
data = client.recv(1024)
print(data.decode('utf-8'))