Python 调用multiprocessing模块下面的Process类方法(实现服务器、客户端并发)-TCP协议

方法一:
服务端:
from
multiprocessing import Process import socket def task(conn): #通信循环 while True: cmd = conn.recv(1024) if len(cmd) == 0:break conn.send(cmd.upper()) print(cmd.decode('utf-8')) if __name__ == '__main__': server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind(('127.0.0.1', 8080)) server.listen(5) # 1.让客户端连进来 # 2.建立连接需要一个单独的 conn,client_addr=server.accept() p=Process(target=task,args=(conn,)) #连接循环 p.start()

--------------------------------------------------------------------------------------------------------
客户端:
import socket
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(('127.0.0.1',8080))

while True:
msg=input('输入你要的信息:')
client.send(msg.encode('utf-8'))
if len(msg) == 0:continue

data=client.recv(1024)
print(data.decode('utf-8'))
client.close()

 

posted @ 2018-07-11 20:54  yangzhizong  阅读(199)  评论(0编辑  收藏  举报