解决python3中自定义wsgi函数,make_server函数报错的问题


Posted in Python onNovember 21, 2017
#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函数报错的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
学习python (1)
Oct 31 Python
在Python的Django框架下使用django-tagging的教程
May 30 Python
python利用拉链法实现字典方法示例
Mar 25 Python
Python正则抓取新闻标题和链接的方法示例
Apr 24 Python
使用apidocJs快速生成在线文档的实例讲解
Feb 07 Python
Python实现自定义顺序、排列写入数据到Excel的方法
Apr 23 Python
python实现两个文件夹的同步
Aug 29 Python
Python语法之精妙的十个知识点(装B语法)
Jan 18 Python
tensorflow 保存模型和取出中间权重例子
Jan 24 Python
解决python中显示图片的plt.imshow plt.show()内存泄漏问题
Apr 24 Python
Python3之乱码\xe6\x97\xa0\xe6\xb3\x95处理方式
May 11 Python
python中adb有什么功能
Jun 07 Python
python不换行之end=与逗号的意思及用途
Nov 21 #Python
python 打印直角三角形,等边三角形,菱形,正方形的代码
Nov 21 #Python
pycharm远程调试openstack代码
Nov 21 #Python
Pycharm远程调试openstack的方法
Nov 21 #Python
pycharm远程调试openstack的图文教程
Nov 21 #Python
Python实现的归并排序算法示例
Nov 21 #Python
Python实现打印螺旋矩阵功能的方法
Nov 21 #Python
You might like
Oracle Faq(Oracle的版本)
2006/10/09 PHP
php curl批处理实现可控并发异步操作示例
2018/05/09 PHP
Laravel框架实现利用监听器进行sql语句记录功能
2018/06/06 PHP
laravel 实现设置时区的简单方法
2019/10/10 PHP
PHP使用POP3读取邮箱接收邮件的示例代码
2020/07/08 PHP
基于JavaScript 下namespace 功能的简单分析
2013/07/05 Javascript
js隐式全局变量造成的bug示例代码
2014/04/22 Javascript
jquery中 $.expr使用实例介绍
2014/06/09 Javascript
javascript正则表达式总结
2016/02/29 Javascript
JS在Chrome浏览器中showModalDialog函数返回值为undefined的解决方法
2016/08/03 Javascript
SVG描边动画
2017/02/23 Javascript
详解Vue 动态添加模板的几种方法
2017/04/25 Javascript
JS实现经典的中国地区三级联动下拉菜单功能实例【测试可用】
2017/06/06 Javascript
nodejs构建本地web测试服务器 如何解决访问静态资源问题
2017/07/14 NodeJs
BootStrap Validator 根据条件在JS中添加或移除校验操作
2017/10/12 Javascript
在Vue项目中取消ESLint代码检测的步骤讲解
2019/01/27 Javascript
[11:27]《一刀刀一天》之DOTA全时刻20:TI4总奖金突破920W TS赛事分析
2014/06/18 DOTA
python任务调度实例分析
2015/05/19 Python
CentOS 6.X系统下升级Python2.6到Python2.7 的方法
2016/10/12 Python
python3下实现搜狗AI API的代码示例
2018/04/10 Python
Python多进程fork()函数详解
2019/02/22 Python
python利用7z批量解压rar的实现
2019/08/07 Python
Python字符串格式化常用手段及注意事项
2020/06/17 Python
python开根号实例讲解
2020/08/30 Python
生物技术专业研究生自荐信
2013/09/22 职场文书
九年级英语教学反思
2014/01/31 职场文书
大队委竞选演讲稿
2014/04/28 职场文书
教师考核材料
2014/05/21 职场文书
校园活动策划方案
2014/06/13 职场文书
保险专业求职信
2014/07/07 职场文书
项目合作意向书模板
2014/07/29 职场文书
先进工作者个人总结
2015/02/15 职场文书
歼十出击观后感
2015/06/11 职场文书
婚礼必备主持词范本!
2019/07/23 职场文书
PostGIS的安装与入门使用指南
2022/01/18 PostgreSQL
Python编程中内置的NotImplemented类型的用法
2022/03/23 Python