我有一个小型服务器程序,它接受TCP或本地UNIX套接字上的连接,读取一个简单的命令,并根据命令发送一个回复.问题是客户端有时可能对答案没兴趣并且提前退出,因此写入该套接字将导致SIGPIPE并使我的服务器崩溃.什么是防止崩溃的最佳做法?有没有办法检查线的另一边是否还在读?(select()似乎在这里不起作用,因为它总是说套接字是可写的).或者我应该用处理程序捕获SIGPIPE并忽略它?
目前我正在使用python内置的应用程序.当我在个人计算机上运行时,它可以正常工作.
但是,当我将它移动到生产服务器时.它不断向我显示如下错误:
我做了一些研究,我得到了最终用户浏览器在服务器仍在忙于发送数据时停止连接的原因.
我想知道它为什么会发生以及阻止它在生产服务器中正常运行的根本原因是什么,而它可以在我的个人计算机上运行.任何建议表示赞赏
Exception happened during processing of request from ('127.0.0.1', 34226)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 284, in
_handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 641, in __init__
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 694, in finish
self.wfile.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
Run Code Online (Sandbox Code Playgroud) 我知道当对端的套接字关闭时会抛出损坏的管道错误.
但是,在我的测试中,我注意到当对等方关闭时,此方立即发送"发送"呼叫并不总是导致管道错误.
例如:
在对等端关闭套接字后(我通过调用close来尝试干净关闭,并且通过查杀对等体也尝试异常关闭),如果我尝试发送40个字节,那么我没有得到破坏的管道,但是,如果我尝试发送40000字节然后它立即给出损坏的管道错误.
究竟是什么原因导致管道断裂并且可以预测它的行为?
我在Nginx后面的远程服务器上有django 1.3.
如果我用apache + mod_wsgi运行django,我可以在apache日志文件中查看错误.没关系,但我想要在控制台中.
如果我运行django自己的开发服务器,只有在DEBUG = False时才会在控制台中出现stacktrace错误.在DEBUG模式控制台输出中
Exception happened during processing of request from (..., ...)
Traceback (most recent call last):
File "/usr/local/python/lib/python2.7/SocketServer.py", line 284, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/local/python/lib/python2.7/SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "/usr/local/python/lib/python2.7/SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/local/python/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 570, in __init__
BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
File "/usr/local/python/lib/python2.7/SocketServer.py", line 641, in __init__
self.finish()
File "/usr/local/python/lib/python2.7/SocketServer.py", line 694, in finish
self.wfile.flush()
File "/usr/local/python/lib/python2.7/socket.py", line 301, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] …
Run Code Online (Sandbox Code Playgroud) 我在python中编写了一个简单的多线程游戏服务器,为每个客户端连接创建一个新线程.我发现时不时,服务器会因为管道损坏/ SIGPIPE错误而崩溃.当程序试图将响应发送回不再存在的客户端时,我非常确定它正在发生.
处理这个问题的好方法是什么?我的首选解决方案只是关闭与客户端的服务器端连接并继续,而不是退出整个程序.
PS:这个问题/答案以一般方式处理问题; 具体应该如何解决?
我试图第一次将代码推送到我的git存储库但是我收到以下错误:
Run Code Online (Sandbox Code Playgroud)Counting objects: 222026, done. Compressing objects: 100% (208850/208850), done. Write failed: Broken pipe222026) error: pack-objects died of signal 13 fatal: The remote end hung up unexpectedly error: failed to push some refs to 'ssh://git@bitbucket.org/<...>'
我试图增加http缓冲区大小(git config http.postBuffer 524288000
),我试过git repack
,但它没有用.
我能够将一个非常相似的大小代码推送到另一个存储库(它不像这个git repack
一样工作,但它确实工作后).我想把它推到bitbucket.
有任何想法吗?
我正在运行Django 1.2开发服务器,每当我使用Chrome或Safari加载页面时,我都会收到这些Broken Pipe错误消息.我的同事在从他的开发服务器加载页面时也会收到错误.使用Opera或Firefox时,我们没有这些错误.
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 281, in run self.finish_response()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 321, in finish_response self.write(data)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 417, in write self._write(data)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 300, in write self.flush()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 286, in flush self._sock.sendall(buffer)
error: [Errno 32] Broken pipe
Run Code Online (Sandbox Code Playgroud)
谁能帮我吗?我为此疯狂!
问题:有没有办法使用flush=True
该print()
功能而无法获得BrokenPipeError
?
我有一个脚本pipe.py
:
for i in range(4000):
print(i)
Run Code Online (Sandbox Code Playgroud)
我从Unix命令行这样称呼它:
python3 pipe.py | head -n3000
Run Code Online (Sandbox Code Playgroud)
它返回:
0
1
2
Run Code Online (Sandbox Code Playgroud)
这个脚本也是如此:
import sys
for i in range(4000):
print(i)
sys.stdout.flush()
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此脚本并将其传递给head -n3000
:
for i in range(4000):
print(i, flush=True)
Run Code Online (Sandbox Code Playgroud)
然后我收到这个错误:
print(i, flush=True)
BrokenPipeError: [Errno 32] Broken pipe
Exception BrokenPipeError: BrokenPipeError(32, 'Broken pipe') in <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'> ignored
Run Code Online (Sandbox Code Playgroud)
我也试过下面的解决方案,但我仍然得到BrokenPipeError
:
import sys
for i in range(4000):
try:
print(i, flush=True)
except BrokenPipeError:
sys.exit()
Run Code Online (Sandbox Code Playgroud) 我在Python中构建了一个简短的url转换器引擎,我看到了一个"破管"错误的TON,我很好奇如何在使用BaseHTTPServer类时最好地捕获它.这不是整个代码,但是让您了解到目前为止我在做什么:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import memcache
class clientThread(BaseHTTPRequestHandler):
def do_GET(self):
content = None
http_code,response_txt,long_url = \
self.ag_trans_url(self.path,content,'GET')
self.http_output( http_code, response_txt, long_url )
return
def http_output(self,http_code,response_txt,long_url):
self.send_response(http_code)
self.send_header('Content-type','text/plain')
if long_url:
self.send_header('Location', long_url)
self.end_headers()
if response_txt:
self.wfile.write(response_txt)
return
def ag_trans_url(self, orig_short_url, post_action, getpost):
short_url = 'http://foo.co' + orig_short_url
# fetch it from memcache
long_url = mc.get(short_url)
# other magic happens to look it up from db if there was nothing
# in memcache, etc
return (302, None, log_url)
def populate_memcache() …
Run Code Online (Sandbox Code Playgroud) 我正在研究一个Django项目.一切顺利,直到我创建了一个Ajax请求,将值从html页面发送到后端(views.py).
当我使用Ajax发送数据时,我能够查看传递给views.py的值,它甚至可以到达render_to_response方法并显示我的页面,但会在终端中抛出损坏的管道错误.我没有看到任何类型的程序中断,但我想知道是否有办法防止此错误发生.我检查了其他回复.但到目前为止没有运气.
当我尝试在刷新的页面上再次点击提交时,我收到以下消息:
您要查找的页面使用了您输入的信息.返回该页面可能会导致您重复执行任何操作.你想继续吗?[提交] [取消]`
这是转储:
Traceback (most recent call last):
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 34812)
----------------------------------------
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 284, in run
self.finish_response()
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 324, in finish_response
self.write(data)
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 403, in write
self.send_headers()
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 467, in send_headers
self.send_preamble()
File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 385, in send_preamble
'Date: %s\r\n' % http_date()
File "/usr/lib/python2.7/socket.py", line 324, in write
self.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
Traceback …
Run Code Online (Sandbox Code Playgroud) broken-pipe ×10
python ×5
django ×3
c ×2
bitbucket ×1
flush ×1
git ×1
io ×1
push ×1
python-2.7 ×1
python-3.x ×1
signals ×1
sigpipe ×1
unix ×1
webkit ×1