解决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 selenium 三种等待方式详解(必会)
Sep 15 Python
python下读取公私钥做加解密实例详解
Mar 29 Python
Python 稀疏矩阵-sparse 存储和转换
May 27 Python
Python算法输出1-9数组形成的结果为100的所有运算式
Nov 03 Python
利用pandas将numpy数组导出生成excel的实例
Jun 14 Python
python将控制台输出保存至文件的方法
Jan 07 Python
Python如何处理大数据?3个技巧效率提升攻略(推荐)
Apr 15 Python
Python OS模块实例详解
Apr 15 Python
windows安装TensorFlow和Keras遇到的问题及其解决方法
Jul 10 Python
pytorch-神经网络拟合曲线实例
Jan 15 Python
python 在sql语句中使用%s,%d,%f说明
Jun 06 Python
用python写PDF转换器的实现
Oct 29 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
PHP未登录自动跳转到登录页面
2016/12/21 PHP
php PDO实现的事务回滚示例
2017/03/23 PHP
详解PHP神奇又有用的Trait
2019/03/25 PHP
myEvent.js javascript跨浏览器事件框架
2011/10/24 Javascript
基于jquery编写分页插件
2016/03/07 Javascript
基于javascript实现句子翻牌网页版小游戏
2016/03/23 Javascript
JavaScript简单下拉菜单特效
2016/09/13 Javascript
javascript中递归的两种写法
2017/01/17 Javascript
解决浏览器会自动填充密码的问题
2017/04/28 Javascript
Axios学习笔记之使用方法教程
2017/07/21 Javascript
vue里面使用mui的弹出日期选择插件实例
2018/09/16 Javascript
微信小程序实现带缩略图轮播效果
2018/11/04 Javascript
js实现左右轮播图
2020/01/09 Javascript
javascript使用正则表达式实现注册登入校验
2020/09/23 Javascript
[01:12:44]VG vs Mineski Supermajor 败者组 BO3 第二场 6.6
2018/06/07 DOTA
Android应用开发中Action bar编写的入门教程
2016/02/26 Python
python类中super()和__init__()的区别
2016/10/18 Python
Python表示矩阵的方法分析
2017/05/26 Python
python中实现指定时间调用函数示例代码
2017/09/08 Python
浅析python打包工具distutils、setuptools
2018/04/20 Python
python3.5 cv2 获取视频特定帧生成jpg图片
2019/08/28 Python
如何基于Python制作有道翻译小工具
2019/12/16 Python
深深扎根运动世界的生活品牌:Tillys
2017/10/30 全球购物
Dune London官网:英国著名奢华鞋履品牌
2017/11/30 全球购物
Can a struct inherit from another struct? (结构体能继承结构体吗)
2016/09/25 面试题
优纳科技软件测试面试题
2012/05/15 面试题
汽车检测与维修专业求职信
2013/10/30 职场文书
面试求职的个人自我评价
2013/11/16 职场文书
应用艺术毕业生的自我评价
2013/12/04 职场文书
检举信的格式及范文
2014/04/04 职场文书
生产车间标语
2014/06/11 职场文书
学习实践科学发展观心得体会
2014/09/10 职场文书
2015年七七事变78周年纪念活动方案
2015/05/06 职场文书
用电申请报告范文
2015/05/18 职场文书
《认识年月日》教学反思
2016/02/19 职场文书
JavaScript严格模式不支持八进制的问题讲解
2021/11/07 Javascript