#coding:utf-8 from wsgiref.simple_server import make_server def RunServer(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) return '<h1>Hello, web!</h1>' if __name__ == '__main__': httpd = make_server('localhost', 8000, RunServer) print ("Serving HTTP on port 8000...") httpd.serve_forever()
这段代码在python2.7中可以运行,到python3.4中运行,就开始报错,报错内容如下:
Serving HTTP on port 8000... 127.0.0.1 - - [12/Apr/2016 16:44:17] "GET / HTTP/1.1" 200 0 Traceback (most recent call last): File "C:\Python34\lib\wsgiref\handlers.py", line 138, in run self.finish_response() File "C:\Python34\lib\wsgiref\handlers.py", line 181, in finish_response self.write(data) File "C:\Python34\lib\wsgiref\handlers.py", line 267, in write "write() argument must be a bytes instance" AssertionError: write() argument must be a bytes instance 127.0.0.1 - - [12/Apr/2016 16:44:17] "GET / HTTP/1.1" 500 59 Traceback (most recent call last): File "C:\Python34\lib\wsgiref\handlers.py", line 138, in run self.finish_response() File "C:\Python34\lib\wsgiref\handlers.py", line 181, in finish_response self.write(data) File "C:\Python34\lib\wsgiref\handlers.py", line 267, in write "write() argument must be a bytes instance" AssertionError: write() argument must be a bytes instance During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Python34\lib\wsgiref\handlers.py", line 141, in run self.handle_error() File "C:\Python34\lib\wsgiref\handlers.py", line 369, in handle_error self.finish_response() File "C:\Python34\lib\wsgiref\handlers.py", line 181, in finish_response self.write(data) File "C:\Python34\lib\wsgiref\handlers.py", line 275, in write self.send_headers() File "C:\Python34\lib\wsgiref\handlers.py", line 332, in send_headers if not self.origin_server or self.client_is_modern(): File "C:\Python34\lib\wsgiref\handlers.py", line 345, 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): ---------------------------------------- Exception happened during processing of request from ('127.0.0.1', 60566) File "C:\Python34\lib\socketserver.py", line 305, in _handle_request_noblock self.process_request(request, client_address) File "C:\Python34\lib\socketserver.py", line 331, in process_request self.finish_request(request, client_address) File "C:\Python34\lib\socketserver.py", line 344, in finish_request self.RequestHandlerClass(request, client_address, self) File "C:\Python34\lib\socketserver.py", line 673, in __init__ self.handle() File "C:\Python34\lib\wsgiref\simple_server.py", line 133, in handle handler.run(self.server.get_app()) File "C:\Python34\lib\wsgiref\handlers.py", line 144, in run self.close() File "C:\Python34\lib\wsgiref\simple_server.py", line 35, in close self.status.split(' ',1)[0], self.bytes_sent AttributeError: 'NoneType' object has no attribute 'split' ---------------------------------------- ---------------------------------------- Exception happened during processing of request from ('127.0.0.1', 60568) ---------------------------------------- 127.0.0.1 - - [12/Apr/2016 16:44:18] "GET / HTTP/1.1" 200 0 Traceback (most recent call last): File "C:\Python34\lib\wsgiref\handlers.py", line 138, in run self.finish_response() File "C:\Python34\lib\wsgiref\handlers.py", line 181, in finish_response self.write(data) File "C:\Python34\lib\wsgiref\handlers.py", line 267, in write "write() argument must be a bytes instance" AssertionError: write() argument must be a bytes instance 127.0.0.1 - - [12/Apr/2016 16:44:18] "GET / HTTP/1.1" 500 59 Traceback (most recent call last): File "C:\Python34\lib\wsgiref\handlers.py", line 138, in run self.finish_response() File "C:\Python34\lib\wsgiref\handlers.py", line 181, in finish_response self.write(data) File "C:\Python34\lib\wsgiref\handlers.py", line 267, in write "write() argument must be a bytes instance" AssertionError: write() argument must be a bytes instance During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Python34\lib\wsgiref\handlers.py", line 141, in run self.handle_error() File "C:\Python34\lib\wsgiref\handlers.py", line 369, in handle_error self.finish_response() File "C:\Python34\lib\wsgiref\handlers.py", line 181, in finish_response self.write(data) File "C:\Python34\lib\wsgiref\handlers.py", line 275, in write self.send_headers() File "C:\Python34\lib\wsgiref\handlers.py", line 332, in send_headers if not self.origin_server or self.client_is_modern(): File "C:\Python34\lib\wsgiref\handlers.py", line 345, 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 "C:\Python34\lib\socketserver.py", line 305, in _handle_request_noblock self.process_request(request, client_address) File "C:\Python34\lib\socketserver.py", line 331, in process_request self.finish_request(request, client_address) File "C:\Python34\lib\socketserver.py", line 344, in finish_request self.RequestHandlerClass(request, client_address, self) File "C:\Python34\lib\socketserver.py", line 673, in __init__ self.handle() File "C:\Python34\lib\wsgiref\simple_server.py", line 133, in handle handler.run(self.server.get_app()) File "C:\Python34\lib\wsgiref\handlers.py", line 144, in run self.close() File "C:\Python34\lib\wsgiref\simple_server.py", line 35, in close self.status.split(' ',1)[0], self.bytes_sent AttributeError: 'NoneType' object has no attribute 'split'
猛地一看,这么多报错,一下就蒙圈了,各种google百度,各种查,google到时能查到一些,
但是英文不好,也看不太明白,百度查到的都是垃圾,根本就没用,最后硬着头皮,一点一点看源码。
首先,根据第一行的提示:
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中自定义wsgi函数,make_server函数报错的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]
- Tank《我不伟大,至少我能改变我。》[FLAC/分轨][236.89MB]
- CD圣经推荐-夏韶声《谙2》SACD-ISO
- 钟镇涛-《百分百钟镇涛》首批限量版SACD-ISO
- 群星《继续微笑致敬许冠杰》[低速原抓WAV+CUE]
- 潘秀琼.2003-国语难忘金曲珍藏集【皇星全音】【WAV+CUE】
- 林东松.1997-2039玫瑰事件【宝丽金】【WAV+CUE】