本文实例讲述了Python提示[Errno 32]Broken pipe导致线程crash错误解决方法。分享给大家供大家参考。具体方法如下:
1. 错误现象
ThreadingHTTPServer 实现的 http 服务,如果客户端在服务器返回前,主动断开连接,则服务器端会报 [Errno 32] Broken pipe 错,并导致处理线程 crash.
下面先看个例子,python 版本: 2.7
示例代码
代码如下:
#!/usr/bin/env python #!coding=utf-8 import os import time import socket import threading from BaseHTTPServer import HTTPServer ,BaseHTTPRequestHandler from SocketServer import ThreadingMixIn class RequestHandler(BaseHTTPRequestHandler): def do_GET(self): """ 处理get请求 """ query=self.path print "query: %s thread=%s" % (query, str(threading.current_thread())) #ret_str="<html>" + self.path + "<br>" + str(self.server) + "<br>" + str(self.responses) + "</html>" ret_str="<html>" + self.path + "<br>" + str(self.server) + "</html>" time.sleep(5) try: self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() self.wfile.write(ret_str) except socket.error, e: print "socket.error : Connection broke. Aborting" + str(e) self.wfile._sock.close() # close socket self.wfile._sock=None return False print "success prod query :%s" % (query) return True #多线程处理 class ThreadingHTTPServer(ThreadingMixIn,HTTPServer): pass if __name__ == '__main__': serveraddr = ('',9001) ser = ThreadingHTTPServer(serveraddr,RequestHandler) ser.serve_forever() sys.exit(0)
运行服务
./thread_http_server_error.py
第1次 curl ,等待返回
代码如下:
[~]$curl -s 'http://10.232.41.142:9001/hello1′ [~]$ 此时服务器端输出日志如下: $./thread_http_server_error.py query: /hello1 thread= search041142.sqa.cm4.tbsite.net – - [15/May/2014 15:02:27] “GET /hello1 HTTP/1.1″ 200 - success prod query :/hello1
第2次 curl ,不等待返回,ctrl +C 来模拟客户端断开
代码如下:
[~]$curl -s 'http://10.232.41.142:9001/hello2′ [~]$ ctrl+C
此时服务器端输出日志如下:
代码如下:
query: /hello2 thread= search041142.sqa.cm4.tbsite.net – - [15/May/2014 15:33:10] “GET /hello2 HTTP/1.1″ 200 - socket.error : Connection broke. Aborting[Errno 32] Broken pipe —————————————- Exception happened during processing of request from ('10.232.41.142′, 48769) Traceback (most recent call last): File “/home/wuzhu/tools/python_2_7_1/lib/python2.7/SocketServer.py”, line 582, in process_request_thread self.finish_request(request, client_address) File “/home/wuzhu/tools/python_2_7_1/lib/python2.7/SocketServer.py”, line 323, in finish_request self.RequestHandlerClass(request, client_address, self) File “/home/wuzhu/tools/python_2_7_1/lib/python2.7/SocketServer.py”, line 639, in __init__ self.handle() File “/home/wuzhu/tools/python_2_7_1/lib/python2.7/BaseHTTPServer.py”, line 337, in handle self.handle_one_request() File “/home/wuzhu/tools/python_2_7_1/lib/python2.7/BaseHTTPServer.py”, line 326, in handle_one_request self.wfile.flush() #actually send the response if not already done. File “/home/wuzhu/tools/python_2_7_1/lib/python2.7/socket.py”, line 303, in flush self._sock.sendall(view[write_offset:write_offset+buffer_size]) AttributeError: 'NoneType' object has no attribute 'sendall'
2. 原因分析
“[Errno 32] Broken pipe “ 产生的原因还是比较明确的,由于 client 在服务器返回前主动断开连接,所以服务器在返回时写 socket 收到SIGPIPE报错。虽然在我们的程序中也对异常进行了处理,将handler 的 wfile._sock 对象close 掉 ,但python 的库里BaseHTTPServer.py中BaseHTTPRequestHandler 类的成员函数handle_one_request还是会直接调用 wfile.flush ,而没有判断 wfile 是否已经 close。
代码如下:
def handle_one_request(self): """Handle a single HTTP request. You normally don't need to override this method; see the class __doc__ string for information on how to handle specific HTTP commands such as GET and POST. """ try: self.raw_requestline = self.rfile.readline(65537) if len(self.raw_requestline) > 65536: self.requestline = '' self.request_version = '' self.command = '' self.send_error(414) return if not self.raw_requestline: self.close_connection = 1 return if not self.parse_request(): # An error code has been sent, just exit return mname = 'do_' + self.command if not hasattr(self, mname): self.send_error(501, "Unsupported method (%r)" % self.command) return method = getattr(self, mname) method() #没有判断 wfile 是否已经 close 就直接调用 flush() self.wfile.flush() #actually send the response if not already done. except socket.timeout, e: #a read or a write timed out. Discard this connection self.log_error("Request timed out: %r", e) self.close_connection = 1 return
3. 解决办法
只要在RequestHandler重载其基类BaseHTTPRequestHandler的成员函数handle_one_reques(),在调用 wfile.flush() 前加上 wfile 是否已经 close 即可。
代码如下:
#!/usr/bin/env python #!coding=utf-8 import os import time import socket import threading from BaseHTTPServer import HTTPServer ,BaseHTTPRequestHandler from SocketServer import ThreadingMixIn class RequestHandler(BaseHTTPRequestHandler): def handle_one_request(self): """Handle a single HTTP request. You normally don't need to override this method; see the class __doc__ string for information on how to handle specific HTTP commands such as GET and POST. """ try: self.raw_requestline = self.rfile.readline(65537) if len(self.raw_requestline) > 65536: self.requestline = '' self.request_version = '' self.command = '' self.send_error(414) return if not self.raw_requestline: self.close_connection = 1 return if not self.parse_request(): # An error code has been sent, just exit return mname = 'do_' + self.command if not hasattr(self, mname): self.send_error(501, "Unsupported method (%r)" % self.command) return method = getattr(self, mname) print "before call do_Get" method() #增加 debug info 及 wfile 判断是否已经 close print "after call do_Get" if not self.wfile.closed: self.wfile.flush() #actually send the response if not already done. print "after wfile.flush()" except socket.timeout, e: #a read or a write timed out. Discard this connection self.log_error("Request timed out: %r", e) self.close_connection = 1 return def do_GET(self): """ 处理get请求 """ query=self.path print "query: %s thread=%s" % (query, str(threading.current_thread())) ret_str="<html>" + self.path + "<br>" + str(self.server) + "</html>" time.sleep(5) try: self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() self.wfile.write(ret_str) except socket.error, e: print "socket.error : Connection broke. Aborting" + str(e) self.wfile._sock.close() self.wfile._sock=None return False print "success prod query :%s" % (query) return True #多线程处理 class ThreadingHTTPServer(ThreadingMixIn,HTTPServer): pass if __name__ == '__main__': serveraddr = ('',9001) ser = ThreadingHTTPServer(serveraddr,RequestHandler) ser.serve_forever() sys.exit(0)
运行服务
./thread_http_server.py
curl ,不等待返回,ctrl +C 来模拟客户端断开
复制代码 代码如下:
[~]$curl -s 'http://10.232.41.142:9001/hello2' [~]$ ctrl+C
此时服务器端输出日志如下:
$./thread_http_server.pybefore call do_Get query: /hello2 thread=<Thread(Thread-1, started 1103210816)> search041142.sqa.cm4.tbsite.net - - [15/May/2014 15:54:09] "GET /hello2 HTTP/1.1" 200 - socket.error : Connection broke. Aborting[Errno 32] Broken pipe after call do_Get after wfile.flush()
以上就是Python提示[Errno 32]Broken pipe导致线程crash错误解决方法的内容,更多相关内容请关注PHP中文网(www.php.cn)!

每天学习Python两个小时是否足够?这取决于你的目标和学习方法。1)制定清晰的学习计划,2)选择合适的学习资源和方法,3)动手实践和复习巩固,可以在这段时间内逐步掌握Python的基本知识和高级功能。

Python在Web开发中的关键应用包括使用Django和Flask框架、API开发、数据分析与可视化、机器学习与AI、以及性能优化。1.Django和Flask框架:Django适合快速开发复杂应用,Flask适用于小型或高度自定义项目。2.API开发:使用Flask或DjangoRESTFramework构建RESTfulAPI。3.数据分析与可视化:利用Python处理数据并通过Web界面展示。4.机器学习与AI:Python用于构建智能Web应用。5.性能优化:通过异步编程、缓存和代码优

Python在开发效率上优于C ,但C 在执行性能上更高。1.Python的简洁语法和丰富库提高开发效率。2.C 的编译型特性和硬件控制提升执行性能。选择时需根据项目需求权衡开发速度与执行效率。

Python在现实世界中的应用包括数据分析、Web开发、人工智能和自动化。1)在数据分析中,Python使用Pandas和Matplotlib处理和可视化数据。2)Web开发中,Django和Flask框架简化了Web应用的创建。3)人工智能领域,TensorFlow和PyTorch用于构建和训练模型。4)自动化方面,Python脚本可用于复制文件等任务。

Python在数据科学、Web开发和自动化脚本领域广泛应用。1)在数据科学中,Python通过NumPy、Pandas等库简化数据处理和分析。2)在Web开发中,Django和Flask框架使开发者能快速构建应用。3)在自动化脚本中,Python的简洁性和标准库使其成为理想选择。

Python的灵活性体现在多范式支持和动态类型系统,易用性则源于语法简洁和丰富的标准库。1.灵活性:支持面向对象、函数式和过程式编程,动态类型系统提高开发效率。2.易用性:语法接近自然语言,标准库涵盖广泛功能,简化开发过程。

Python因其简洁与强大而备受青睐,适用于从初学者到高级开发者的各种需求。其多功能性体现在:1)易学易用,语法简单;2)丰富的库和框架,如NumPy、Pandas等;3)跨平台支持,可在多种操作系统上运行;4)适合脚本和自动化任务,提升工作效率。

可以,在每天花费两个小时的时间内学会Python。1.制定合理的学习计划,2.选择合适的学习资源,3.通过实践巩固所学知识,这些步骤能帮助你在短时间内掌握Python。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

记事本++7.3.1
好用且免费的代码编辑器