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

#基于UDP协议的multiprocessing自定义通信
服务端:
from multiprocessing import Process
import socket

def task(server):    #通信循环
    while True:
        data, client_addr = server.recvfrom(1024)
        print('===>', data, client_addr)
        server.sendto(data.upper(), client_addr)

if __name__ == '__main__':
    server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # 数据报协议-》udp
    server.bind(('127.0.0.1', 8080))
    # data, client_addr = server.recvfrom(1024)
    p=Process(target=task,args=(server,))   #连接循环
    p.start()

--------------------------------------------------------------------------------
客户端:

import socket

client=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) #数据报协议-》udp

while True:
    msg=input('>>: ').strip() #msg=''
    client.sendto(msg.encode('utf-8'),('127.0.0.1',8080))
    data,server_addr=client.recvfrom(1024)
    print(data)
    
client.close()

 

posted @ 2018-07-11 21:46  yangzhizong  阅读(460)  评论(0编辑  收藏  举报