您的位置:首页 > 编程语言 > Python开发

python socket:[Errno 32] Broken pipe

2013-09-14 18:12 369 查看
这个错误发生在当client端close了当前与你的server端的socket连接,但是你的server端在忙着发送数据给一个已经断开连接的socket。

下面是stackoverflow给的解决方案:

Your server process has received a SIGPIPE writing to a socket. This usually happens when you write to a socket fully closed on the other (client) side. This might be happening when a client program doesn't wait till all the data from the server is received and simply closes a socket (using close function). In a C program you would normally try setting to ignore SIGPIPE signal or setting a dummy signal handler for it. In this case a simple error will be returned when writing to a closed socket. In your case a python seems to throw an exception that can be handled as a premature disconnect of the client.
下面是python的实现:
import socket
def hack_fileobject_close():
if getattr(socket._fileobject.close, '__hacked__', None):
return
old_close = socket._fileobject.close
def new_close(self, *p, **kw):
try:
return old_close(self, *p, **kw)
except Exception, e:
print("Ignore %s." % str(e))
new_close.__hacked__ = True
socket._fileobject.close = new_close
hack_fileobject_close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: