服务端:
from socket import * from threading import Thread from concurrent.futures import ThreadPoolExecutor def communicate(conn): while True: try: data=conn.recv(1024) if not data:break conn.send(data.upper()) except ConnectionResetError: break conn.close() def server(ip,port, pool_size): pool = ThreadPoolExecutor(pool_size) server = socket(AF_INET, SOCK_STREAM) server.bind((ip,port)) server.listen(5) while True: conn, addr = server.accept() # t=Thread(target=communicate,args=(conn,)) # t.start() pool.submit(communicate, conn) server.close() if __name__ == ‘__main__‘: server(‘127.0.0.1‘, 8082, 2)
客户端: from socket import * client=socket(AF_INET,SOCK_STREAM) client.connect((‘127.0.0.1‘,8082)) while True: msg=input(‘>>: ‘).strip() if not msg:continue client.send(msg.encode(‘utf-8‘)) data=client.recv(1024) print(data.decode(‘utf-8‘)) client.close()
原文地址:https://www.cnblogs.com/beallaliu/p/9192809.html
时间: 2024-10-09 19:37:04