解决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中asyncore的用法实例
Sep 29 Python
Python字符串格式化
Jun 15 Python
开始着手第一个Django项目
Jul 15 Python
简单谈谈Python中的反转字符串问题
Oct 24 Python
python中正则的使用指南
Dec 04 Python
Python简单的制作图片验证码实例
May 31 Python
python基于C/S模式实现聊天室功能
Jan 09 Python
Python 窗体(tkinter)按钮 位置实例
Jun 13 Python
基于SpringBoot构造器注入循环依赖及解决方式
Apr 26 Python
python3通过qq邮箱发送邮件以及附件
May 20 Python
详解Python 循环嵌套
Jul 09 Python
python爬虫中采集中遇到的问题整理
Nov 27 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初学者写及时补给skype用户充话费的小程序
2008/11/02 PHP
深入分析使用mysql_fetch_object()以对象的形式返回查询结果
2013/06/05 PHP
php字符编码转换之gb2312转为utf8
2013/10/28 PHP
php实现过滤表单提交中html标签的方法
2014/10/17 PHP
ThinkPHP3.1.x修改成功与失败跳转页面的方法
2017/09/29 PHP
js利用div背景,做一个竖线的效果。
2008/11/22 Javascript
js 发个判断字符串是否为符合标准的函数
2009/04/27 Javascript
用jQuery与JSONP轻松解决跨域访问的问题
2014/02/04 Javascript
易操作的jQuery表单提示插件
2015/12/01 Javascript
学习Angularjs分页指令
2016/07/01 Javascript
在iframe中使bootstrap的模态框在父页面弹出问题
2017/08/07 Javascript
详解操作虚拟dom模拟react视图渲染
2018/07/25 Javascript
JS构造一个html文本内容成文件流形式发送到后台
2018/07/31 Javascript
vue中rem的配置的方法示例
2018/08/30 Javascript
Vue开发环境中修改端口号的实现方法
2019/08/15 Javascript
JS实现利用闭包判断Dom元素和滚动条的方向示例
2019/08/26 Javascript
countUp.js实现数字动态变化效果
2019/10/17 Javascript
如何基于jQuery实现五角星评分
2020/09/02 jQuery
Node.js fs模块原理及常见用途
2020/10/22 Javascript
Python实现数据库编程方法详解
2015/06/09 Python
Django中传递参数到URLconf的视图函数中的方法
2015/07/18 Python
Python单链表简单实现代码
2016/04/27 Python
浅谈python 线程池threadpool之实现
2017/11/17 Python
Python人脸识别初探
2017/12/21 Python
Python request设置HTTPS代理代码解析
2018/02/12 Python
利用pandas读取中文数据集的方法
2018/07/25 Python
python实现给scatter设置颜色渐变条colorbar的方法
2018/12/13 Python
python实现合并两个排序的链表
2019/03/03 Python
瑞士隐形眼镜和护理产品网上商店:Linsenklick
2019/10/21 全球购物
WEB控件可以激发服务端事件,请谈谈服务端事件是怎么发生并解释其原理?自动传回是什么?为什么要使用自动传回?
2012/02/21 面试题
EJB发布WEB服务一般步骤
2012/10/31 面试题
会计岗位职责范本
2014/03/07 职场文书
一年级数学上册复习计划
2015/01/17 职场文书
研究生给导师的自荐信
2015/03/06 职场文书
SpringBoot系列之MongoDB Aggregations用法详解
2022/02/12 MongoDB
MySQL中JOIN连接的基本用法实例
2022/06/05 MySQL