Python3网络编程在构造流媒体或其他’Content-Type’类型响应接口时发生如下报错信息:
Traceback (most recent call last):
File "/usr/lib/python3.6/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/usr/lib/python3.6/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/usr/lib/python3.6/wsgiref/handlers.py", line 279, in write
self._write(data)
File "/usr/lib/python3.6/wsgiref/handlers.py", line 453, in _write
result = self.stdout.write(data)
File "/usr/lib/python3.6/socketserver.py", line 803, in write
self._sock.sendall(b)
ConnectionResetError: [Errno 104] Connection reset by peer
ERROR basehttp 124 "GET /control/v1/video_address?file_path=/fsdata/videos/1.mp4/h264_1.MP4 HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('192.168.2.63', 58153)
Traceback (most recent call last):
File "/usr/lib/python3.6/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/usr/lib/python3.6/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/usr/lib/python3.6/wsgiref/handlers.py", line 279, in write
self._write(data)
File "/usr/lib/python3.6/wsgiref/handlers.py", line 453, in _write
result = self.stdout.write(data)
File "/usr/lib/python3.6/socketserver.py", line 803, in write
self._sock.sendall(b)
ConnectionResetError: [Errno 104] Connection reset by peer
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.6/wsgiref/handlers.py", line 141, in run
self.handle_error()
File "/usr/local/lib/python3.6/dist-packages/django/core/servers/basehttp.py", line 88, in handle_error
super(ServerHandler, self).handle_error()
File "/usr/lib/python3.6/wsgiref/handlers.py", line 368, in handle_error
self.finish_response()
File "/usr/lib/python3.6/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/usr/lib/python3.6/wsgiref/handlers.py", line 274, in write
self.send_headers()
File "/usr/lib/python3.6/wsgiref/handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "/usr/lib/python3.6/wsgiref/handlers.py", line 344, in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.6/socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib/python3.6/socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python3.6/socketserver.py", line 724, in __init__
self.handle()
File "/usr/local/lib/python3.6/dist-packages/django/core/servers/basehttp.py", line 155, in handle
handler.run(self.server.get_app())
File "/usr/lib/python3.6/wsgiref/handlers.py", line 144, in run
self.close()
File "/usr/lib/python3.6/wsgiref/simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
经测试这个异常不影响程序运行及接口数据的返回,但抛出这么多异常看的我是浑身难受,而且项目日志也没必要收集这么多无用的异常影响其他异常的定位,当然也可以忽略这个异常不收集,但强迫症患者只考虑解如何决这个问题,网上寻找解决方案,大都是一些没有营养的垃圾内容,最后自己跟进程序查看底层源码,解决流程如下:
首先,根据第一行的提示:
File "C:\Python34\lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
我这里用的编辑器是pycharm,找到handlers.py文件的138行,按住ctrl点击文件中的finish_response()方法,
就找到self.finish_response()定义的位置了。根据第二条提示:
File "C:\Python34\lib\wsgiref\handlers.py", line 181, in finish_response
self.write(data)
发现是write方法出现错误,再按住ctrl点击write方法,找到定义write方法的位置,发现第一行就定义了一条报错:
assert type(data) is bytes, \
"write() argument must be a bytes instance"
对照上面的报错信息,发现可能是变量data的类型,不是bytes,所以在handlers.py181行代码self.write(data)上面加一句:
data=data.encode()
修改时会提示,选择强制修改即可
再次刷新程序,发现所有报错都没了,程序正常运行。
版权声明:本文为file_data原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。