Python socket.error: [Errno 32] 断管
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41014252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
socket.error: [Errno 32] Broken pipe
提问by ArnabC
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:
我写了一个客户端 - 服务器 python 程序,客户端向服务器发送一个列表,服务器接收数组,删除列表的前两个元素并将其发送回客户端。服务器接收列表没有问题。但是当服务器想要发回编辑后的列表时,它显示错误:
socket.error: [Errno 32] Broken pipe
。client.py 和 server.py 在不同的机器上运行,具有不同的 ip。我在下面发布了 client.py 和 server.py 的代码:
Client.py
客户端.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;
Server.py:
服务器.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();
The entire error msg is:
整个错误消息是:
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.
如何解决此问题,以便客户端可以从服务器接收编辑后的列表而不会出现任何错误?先感谢您。
回答by Stan Vanhoorn
You made a small mistake:
你犯了一个小错误:
s.send(data_string1);
Should be:
应该:
conn.send(data_string1);
Also the following lines need to be changed:
还需要更改以下几行:
socket.shutdown();
to s.shutdown();
socket.shutdown();
到 s.shutdown();
And:
和:
socket.close();
to s.close();
socket.close();
到 s.close();