基于多进程 server 端 同时处理多个socket客户端
from multiprocessing import Processfrom socket import *import timeserver=socket(AF_INET,SOCK_STREAM)server.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)server.bind((‘127.0.0.1‘,80))server.listen(5)def sock_server(conn,addr): while True: try: data=conn.recv(1024) if not data:continue print(data.decode(‘utf-8‘)) conn.send(data.upper()) except Exception as e: print(e) break# conn.close()# server.close()if __name__ == ‘__main__‘: while True: conn,addr=server.accept() p1=Process(target=sock_server,args=(conn,addr)) p1.start()
时间: 2024-10-25 02:48:13