解决python3中自定义wsgi函数,make_server函数报错的问题
#coding:utf-8
fromwsgiref.simple_serverimportmake_server
defRunServer(environ,start_response):
start_response('200OK',[('Content-Type','text/html')])
return'Hello,web!
'
if__name__=='__main__':
httpd=make_server('localhost',8000,RunServer)
print("ServingHTTPonport8000...")
httpd.serve_forever()
这段代码在python2.7中可以运行,到python3.4中运行,就开始报错,报错内容如下:
ServingHTTPonport8000...
127.0.0.1--[12/Apr/201616:44:17]"GET/HTTP/1.1"2000
Traceback(mostrecentcalllast):
File"C:\Python34\lib\wsgiref\handlers.py",line138,inrun
self.finish_response()
File"C:\Python34\lib\wsgiref\handlers.py",line181,infinish_response
self.write(data)
File"C:\Python34\lib\wsgiref\handlers.py",line267,inwrite
"write()argumentmustbeabytesinstance"
AssertionError:write()argumentmustbeabytesinstance
127.0.0.1--[12/Apr/201616:44:17]"GET/HTTP/1.1"50059
Traceback(mostrecentcalllast):
File"C:\Python34\lib\wsgiref\handlers.py",line138,inrun
self.finish_response()
File"C:\Python34\lib\wsgiref\handlers.py",line181,infinish_response
self.write(data)
File"C:\Python34\lib\wsgiref\handlers.py",line267,inwrite
"write()argumentmustbeabytesinstance"
AssertionError:write()argumentmustbeabytesinstance
Duringhandlingoftheaboveexception,anotherexceptionoccurred:
Traceback(mostrecentcalllast):
File"C:\Python34\lib\wsgiref\handlers.py",line141,inrun
self.handle_error()
File"C:\Python34\lib\wsgiref\handlers.py",line369,inhandle_error
self.finish_response()
File"C:\Python34\lib\wsgiref\handlers.py",line181,infinish_response
self.write(data)
File"C:\Python34\lib\wsgiref\handlers.py",line275,inwrite
self.send_headers()
File"C:\Python34\lib\wsgiref\handlers.py",line332,insend_headers
ifnotself.origin_serverorself.client_is_modern():
File"C:\Python34\lib\wsgiref\handlers.py",line345,inclient_is_modern
returnself.environ['SERVER_PROTOCOL'].upper()!='HTTP/0.9'
TypeError:'NoneType'objectisnotsubscriptable
Duringhandlingoftheaboveexception,anotherexceptionoccurred:
Traceback(mostrecentcalllast):
----------------------------------------
Exceptionhappenedduringprocessingofrequestfrom('127.0.0.1',60566)
File"C:\Python34\lib\socketserver.py",line305,in_handle_request_noblock
self.process_request(request,client_address)
File"C:\Python34\lib\socketserver.py",line331,inprocess_request
self.finish_request(request,client_address)
File"C:\Python34\lib\socketserver.py",line344,infinish_request
self.RequestHandlerClass(request,client_address,self)
File"C:\Python34\lib\socketserver.py",line673,in__init__
self.handle()
File"C:\Python34\lib\wsgiref\simple_server.py",line133,inhandle
handler.run(self.server.get_app())
File"C:\Python34\lib\wsgiref\handlers.py",line144,inrun
self.close()
File"C:\Python34\lib\wsgiref\simple_server.py",line35,inclose
self.status.split('',1)[0],self.bytes_sent
AttributeError:'NoneType'objecthasnoattribute'split'
----------------------------------------
----------------------------------------
Exceptionhappenedduringprocessingofrequestfrom('127.0.0.1',60568)
----------------------------------------
127.0.0.1--[12/Apr/201616:44:18]"GET/HTTP/1.1"2000
Traceback(mostrecentcalllast):
File"C:\Python34\lib\wsgiref\handlers.py",line138,inrun
self.finish_response()
File"C:\Python34\lib\wsgiref\handlers.py",line181,infinish_response
self.write(data)
File"C:\Python34\lib\wsgiref\handlers.py",line267,inwrite
"write()argumentmustbeabytesinstance"
AssertionError:write()argumentmustbeabytesinstance
127.0.0.1--[12/Apr/201616:44:18]"GET/HTTP/1.1"50059
Traceback(mostrecentcalllast):
File"C:\Python34\lib\wsgiref\handlers.py",line138,inrun
self.finish_response()
File"C:\Python34\lib\wsgiref\handlers.py",line181,infinish_response
self.write(data)
File"C:\Python34\lib\wsgiref\handlers.py",line267,inwrite
"write()argumentmustbeabytesinstance"
AssertionError:write()argumentmustbeabytesinstance
Duringhandlingoftheaboveexception,anotherexceptionoccurred:
Traceback(mostrecentcalllast):
File"C:\Python34\lib\wsgiref\handlers.py",line141,inrun
self.handle_error()
File"C:\Python34\lib\wsgiref\handlers.py",line369,inhandle_error
self.finish_response()
File"C:\Python34\lib\wsgiref\handlers.py",line181,infinish_response
self.write(data)
File"C:\Python34\lib\wsgiref\handlers.py",line275,inwrite
self.send_headers()
File"C:\Python34\lib\wsgiref\handlers.py",line332,insend_headers
ifnotself.origin_serverorself.client_is_modern():
File"C:\Python34\lib\wsgiref\handlers.py",line345,inclient_is_modern
returnself.environ['SERVER_PROTOCOL'].upper()!='HTTP/0.9'
TypeError:'NoneType'objectisnotsubscriptable
Duringhandlingoftheaboveexception,anotherexceptionoccurred:
Traceback(mostrecentcalllast):
File"C:\Python34\lib\socketserver.py",line305,in_handle_request_noblock
self.process_request(request,client_address)
File"C:\Python34\lib\socketserver.py",line331,inprocess_request
self.finish_request(request,client_address)
File"C:\Python34\lib\socketserver.py",line344,infinish_request
self.RequestHandlerClass(request,client_address,self)
File"C:\Python34\lib\socketserver.py",line673,in__init__
self.handle()
File"C:\Python34\lib\wsgiref\simple_server.py",line133,inhandle
handler.run(self.server.get_app())
File"C:\Python34\lib\wsgiref\handlers.py",line144,inrun
self.close()
File"C:\Python34\lib\wsgiref\simple_server.py",line35,inclose
self.status.split('',1)[0],self.bytes_sent
AttributeError:'NoneType'objecthasnoattribute'split'
猛地一看,这么多报错,一下就蒙圈了,各种google百度,各种查,google到时能查到一些,
但是英文不好,也看不太明白,百度查到的都是垃圾,根本就没用,最后硬着头皮,一点一点看源码。
首先,根据第一行的提示:
File"C:\Python34\lib\wsgiref\handlers.py",line138,inrun self.finish_response()
我这里用的编辑器是pycharm,找到handlers.py文件的138行,按住ctrl点击文件中的finish_response()方法,
就找到self.finish_response()定义的位置了。根据第二条提示:
File"C:\Python34\lib\wsgiref\handlers.py",line181,infinish_response self.write(data)
发现是write方法出现错误,再按住ctrl点击write方法,找到定义write方法的位置,发现第一行就定义了一条报错:
asserttype(data)isbytes,\ "write()argumentmustbeabytesinstance"
对照上面的报错信息,发现可能是变量data的类型,不是bytes,所以在handlers.py181行代码self.write(data)上面加一句:
data=data.encode(),再次刷新程序,发现所有报错居然都没了,程序正常运行。
以上这篇解决python3中自定义wsgi函数,make_server函数报错的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。