解决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的dataframe转换为多维矩阵的方法
Apr 11 Python
python中的二维列表实例详解
Jun 19 Python
Python 从列表中取值和取索引的方法
Dec 25 Python
python3.x实现base64加密和解密
Mar 28 Python
pip指定python位置安装软件包的方法
Jul 12 Python
原生python实现knn分类算法
Oct 24 Python
Django框架反向解析操作详解
Nov 28 Python
python新手学习使用库
Jun 11 Python
套娃式文件夹如何通过Python批量处理
Aug 23 Python
Python下使用Trackbar实现绘图板
Oct 27 Python
pygame面向对象的飞行小鸟实现(Flappy bird)
Apr 01 Python
Python并发编程实例教程之线程的玩法
Jun 20 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开发规范手册之PHP代码规范详解
2011/01/13 PHP
从刷票了解获得客户端IP的方法
2015/09/21 PHP
php设计模式之策略模式应用案例详解
2019/06/17 PHP
Laravel 5.5 实现禁用用户注册示例
2019/10/24 PHP
JavaScript学习笔记(十七)js 优化
2010/02/04 Javascript
js 判断图片是否加载完以及实现图片的预下载
2014/08/14 Javascript
ie8模式下click无反应点击option无反应的解决方法
2014/10/11 Javascript
js实现的牛顿摆效果
2015/03/31 Javascript
javascript中substring()、substr()、slice()的区别
2015/08/30 Javascript
jQuery简单实现仿京东商城的左侧菜单效果代码
2015/09/09 Javascript
JavaScript使用链式方法封装jQuery中CSS()方法示例
2017/04/07 jQuery
vue脚手架vue-cli的学习使用教程
2017/06/06 Javascript
浅谈mint-ui 填坑之路
2017/11/06 Javascript
express+mockjs实现模拟后台数据发送功能
2018/01/07 Javascript
vue的全局提示框组件实例代码
2018/02/26 Javascript
基于Vuejs的搜索匹配功能实现方法
2018/03/03 Javascript
微信小程序时间控件picker view使用详解
2018/12/28 Javascript
vuex实现像调用模板方法一样调用Mutations方法
2019/11/06 Javascript
Vue移动端用淘宝弹性布局lib-flexible插件做适配的方法
2020/05/26 Javascript
[01:12]DOTA2 2015年秋季互动指南
2015/11/10 DOTA
[47:06]DOTA2上海特级锦标赛主赛事日 - 4 败者组第五轮 MVP.Phx VS EG第一局
2016/03/05 DOTA
Django 如何获取前端发送的头文件详解(推荐)
2017/08/15 Python
Django中在xadmin中集成DjangoUeditor过程详解
2019/07/24 Python
伯克斯奥特莱斯:Burkes Outlet
2019/03/30 全球购物
施华洛世奇新加坡官网:SWAROVSKI新加坡
2020/10/06 全球购物
神路信息Java面试题目
2013/03/31 面试题
小学岗位竞聘方案
2014/01/22 职场文书
全神贯注教学反思
2014/02/03 职场文书
青春励志演讲稿
2014/04/29 职场文书
星级党支部申报材料
2014/05/31 职场文书
入党积极分子学习党的纲领思想汇报
2014/09/13 职场文书
乡镇党委书记个人整改措施
2014/09/15 职场文书
私用公车造成事故检讨书
2014/11/16 职场文书
给老婆的保证书
2015/01/16 职场文书
python基础学习之递归函数知识总结
2021/05/26 Python
这样写python注释让代码更加的优雅
2021/06/02 Python