TA的每日心情 | 擦汗 昨天 09:07 |
---|
签到天数: 527 天 连续签到: 4 天 [LV.9]测试副司令
|
1测试积点
Python多进程问题的报错问题
想通过多进程实现客户端给服务器发送信息,服务器同时接收多个信息。但是会收到以下报错:
OSError: [WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。
看网上说是进程停止又重启造成的结果
贴下自己的代码,麻烦大家看下
- <p>import multiprocessing
- from socket import
- import time</p><p>server_name='172.20.53.211'
- port_number1=12002
- port_number2=12003</p><p><p>def process_one():
- server_socket1 = socket(AF_INET, SOCK_DGRAM)
- server_socket1.bind(('', port_number1))
- data_receive1, client_address = server_socket1.recvfrom(1024)
- server_socket1.close()
- print(data_receive1,1)</p><p>def process_two():
- server_socket2 = socket(AF_INET, SOCK_DGRAM)
- server_socket2.bind(('', port_number2))
- data_receive2, client_address = server_socket2.recvfrom(1024)
- server_socket2.close()
- print(data_receive2,2)</p><p>
- p1=multiprocessing.Process(target=process_one())
- p1.start()
- p2=multiprocessing.Process(target=process_two())
- p2.start()</p><p>以上是服务器端的代码</p><p>以下是客户端发送代码</p><p>from socket import *
- import time
- server_name='172.20.53.211'
- port_number1=12002
- port_number2=12003
- client_socket = socket(AF_INET, SOCK_DGRAM)
- f1=open('1.txt','rb')
- fcontent=f1.read()
- client_socket.sendto(fcontent,(server_name, port_number1))
- time.sleep(0.2)
- client_socket.sendto('a'.encode('UTF-8'),(server_name, port_number2))
- client_socket.close()</p>
复制代码
求大佬指出哪里错了,谢谢
|
|