搜索
简帛阁>技术文章>AttributeError: 'NoneType' object has no attribute 'split'问题处理

AttributeError: 'NoneType' object has no attribute 'split'问题处理

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()

修改时会提示,选择强制修改即可



再次刷新程序,发现所有报错都没了,程序正常运行。

Python3网络编程在构造流媒体或其他’ContentType’类型响应接口时发生如下报错信息:Traceback(mostrecentcalllast):File/usr/lib/python36
Python如何解决’NoneType’objecthasnoattribute'…'的问题用Python+selenium和Beautifulsoup爬取MOCC中国大学慕课网上某网上课程的课堂评
运行Python连接mysql数据,查询表格中的数据时报了如下错误AttributeError:'NoneType'objecthasnoattribute'execute'的解决方案pythonge
先把出错的代码贴出来all_handlesactionwindow_handlesforhandleinall_handles:ifhandle!orginal_page:actionswitch_t
今天看python爬虫的视频,然后按照视频上的内容编写代码,运行时出现了AttributeError:'NoneType'objecthasnoattribute'children'错误提示,看了好久
环境ubuntu1804python3错误信息Traceback(mostrecentcalllast):File/home/w/local/lib/python36/sitepackages/mat
在写python脚本时遇到AttributeError:'NoneType'objecthasnoattribute'append'a[]b[1,2,3,4]aaappend(b)执行一次后发现a的类
pythonpip'nonetype'objecthasnoattribute'bytes'更新pipforWindows:pythonmpipinstallUpipforLinux:pipinsta
总之就是很蠢的问题,作者本身的代码路径有问题,首先数据集路径必须按照官网设置对,注意大小写:其次我还是报错了,仔细一看:好家伙,路径少了个cityscapes,所以直接用绝对路径,在yaml和cit
Python在操作Mysql数据的时候,报了两个错误,一个是AttributeError:'DBHelper'objecthasnoattribute'cursor';另一个是%dformat:anu