多进程实现socket通信(Python)
服务器端:
#__author__:Kelvin #date:2020/5/9 11:35 import socket from multiprocessing import Process def talk(conn): while True: msg = conn.recv(1024).decode("utf-8") conn.send(msg.upper().encode("utf-8")) print(msg) if __name__=="__main__": ip_port = ("127.0.0.1", 9001) sk = socket.socket() sk.bind(ip_port) sk.listen() while True: print("新的连接。。。") conn, addr = sk.accept() Process(target=talk,args=(conn,)).start() conn.close() sk.close()
客户端:
#__author__:Kelvin #date:2020/5/9 11:35 import socket import time ip_port=("127.0.0.1",9001) sk=socket.socket() sk.connect(ip_port) while True: sk.send(b"hello kelvin!") msg = sk.recv(1024).decode("utf-8") print(msg) time.sleep(1) sk.close()