socket.error: [Errno 32] Broken pipe(socket.error: [Errno 32] 管道损坏)
问题描述
我写了一个客户端-服务器python程序,客户端向服务器发送一个列表,服务器接收数组,删除列表的前两个元素并将其发送回客户端.服务器接收列表没有问题.但是当服务器想要发回编辑后的列表时,它会显示错误:socket.error: [Errno 32] 损坏的管道.client.py 和 server.py 运行在具有不同 ip 的不同机器上.我在下面发布了 client.py 和 server.py 的代码:
I wrote a client-server python program where the client sends a list to the server, the server receives the array, deletes the first two elements of the list and sends it back to the client.
There is no problem with the server receiving the list. But when the server wants to send back the edited list, it is showing error:
socket.error: [Errno 32] Broken pipe.
The client.py and the server.py are running from different machines with different ip. I'm posting the code for the client.py and server.py below:
客户端.py
import socket, pickle
HOST = '192.168.30.218'
PORT = 50010
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
arr = ['CS','UserMgmt','AddUser','Arnab','Password']
data_string = pickle.dumps(arr)
s.send(data_string)
data = s.recv(4096)
data_arr1 = pickle.loads(data)
s.close()
print 'Received', repr(data_arr1)
print data_arr1;
服务器.py:
import socket, pickle;
HOST = '127.0.0.1';
PORT = 50010;
s= socket.socket(socket.AF_INET, socket.SOCK_STREAM);
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1);
s.bind(('',PORT));
s.listen(1);
conn, addr = s.accept();
print 'Connected by' , addr;
data_addr = list();
while 1:
data = conn.recv(4096);
if not data: break;
data_addr = pickle.loads(data);
print 'Received Data', repr(data_addr);
print data_addr;
data_addr.pop(0);
data_addr.pop(0);
print data_addr;
data_string1 = pickle.dumps(data_addr);
s.send(data_string1);
break;
conn.close();
socket.shutdown();
socket.close();
整个错误信息是:
Traceback (most recent call last):
File "server.py", line 22, in <module>
s.send(data_string1);
socket.error: [Errno 32] Broken pipe
我该如何解决这个问题,以便客户端可以从服务器接收编辑后的列表而不会出现任何错误?提前谢谢你.
How do I fix this problem so that the client can receive the edited list from the server without any error ? Thank You in advance.
推荐答案
你犯了一个小错误:
s.send(data_string1);
应该是:
conn.send(data_string1);
还需要更改以下几行:
socket.shutdown(); 到 s.shutdown();
还有:
socket.close(); 到 s.close();
这篇关于socket.error: [Errno 32] 管道损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:socket.error: [Errno 32] 管道损坏
基础教程推荐
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
