解决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 Grid使用和布局详解
Jun 30 Python
win10下python3.5.2和tensorflow安装环境搭建教程
Sep 19 Python
Python格式化输出字符串方法小结【%与format】
Oct 29 Python
在Python中调用Ping命令,批量IP的方法
Jan 26 Python
Python Gluon参数和模块命名操作教程
Dec 18 Python
pytorch 实现tensor与numpy数组转换
Dec 27 Python
pycharm通过ssh连接远程服务器教程
Feb 12 Python
Python3监控疫情的完整代码
Feb 20 Python
Python如何爬取51cto数据并存入MySQL
Aug 25 Python
利用python清除移动硬盘中的临时文件
Oct 28 Python
python 实现ping测试延迟的两种方法
Dec 10 Python
给numpy.array增加维度的超简单方法
Jun 02 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 魔术方法使用说明
2009/10/20 PHP
php access 数据连接与读取保存编辑数据的实现代码
2010/05/12 PHP
php array_intersect比array_diff快(附详细的使用说明)
2011/07/03 PHP
php引用返回与取消引用的详解
2013/06/08 PHP
PHP开发微信支付的代码分享
2014/05/25 PHP
php禁用函数设置及查看方法详解
2016/07/25 PHP
php使用gearman进行任务分发操作实例详解
2020/02/26 PHP
PHP 与 js的通信(via ajax,json)
2010/11/16 Javascript
js获取input标签的输入值实现代码
2013/08/05 Javascript
javascript实现原生ajax的几种方法介绍
2013/09/21 Javascript
javascript使用switch case实现动态改变超级链接文字及地址
2014/12/16 Javascript
jquery.Callbacks的实现详解
2016/11/30 Javascript
JS调用Android、Ios原生控件
2017/01/06 Javascript
layui的表单验证支持ajax判断用户名是否重复的实例
2019/09/06 Javascript
vite2.0+vue3移动端项目实战详解
2021/03/03 Vue.js
[03:07]2015国际邀请赛选手档案EHOME.rOtK 是什么让他落泪?
2015/07/31 DOTA
[41:12]Liquid vs Secret 2019国际邀请赛淘汰赛 败者组 BO3 第一场 8.24
2019/09/10 DOTA
Python脚本实现12306火车票查询系统
2016/09/30 Python
python爬虫之urllib3的使用示例
2018/07/09 Python
python实现日志按天分割
2019/07/22 Python
Pytorch 使用opnecv读入图像由HWC转为BCHW格式方式
2020/06/02 Python
使用keras实现Precise, Recall, F1-socre方式
2020/06/15 Python
基于HTML5+Webkit实现树叶飘落动画
2017/12/28 HTML / CSS
手工制作的豪华英式沙发和沙发床:Willow & Hall
2019/05/03 全球购物
PatPat阿根廷:妈妈们的购物平台
2019/05/30 全球购物
工程安全员岗位职责
2014/03/09 职场文书
市场营销计划书范文
2015/01/16 职场文书
项目技术负责人岗位职责
2015/04/13 职场文书
计划生育责任书
2015/05/09 职场文书
入党培养人考察意见
2015/06/08 职场文书
2015年度对口支援工作总结
2015/07/22 职场文书
2016毕业实习单位评语大全
2015/12/01 职场文书
小学语文教学反思范文
2016/03/03 职场文书
golang在GRPC中设置client的超时时间
2021/04/27 Golang
python 爬取华为应用市场评论
2021/05/29 Python
Python多线程 Queue 模块常见用法
2021/07/04 Python