解決python3中自定義wsgi函數(shù),make_server函數(shù)報錯的問題
#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中可以運(yùn)行,到python3.4中運(yùn)行,就開始報錯,報錯內(nèi)容如下:
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到時能查到一些,
但是英文不好,也看不太明白,百度查到的都是垃圾,根本就沒用,最后硬著頭皮,一點一點看源碼。
首先,根據(jù)第一行的提示:
File "C:\Python34\lib\wsgiref\handlers.py", line 138, in run self.finish_response()
我這里用的編輯器是pycharm,找到handlers.py文件的138行,按住ctrl點擊文件中的finish_response()方法,
就找到self.finish_response()定義的位置了。根據(jù)第二條提示:
File "C:\Python34\lib\wsgiref\handlers.py", line 181, in finish_response self.write(data)
發(fā)現(xiàn)是write方法出現(xiàn)錯誤,再按住ctrl點擊write方法,找到定義write方法的位置,發(fā)現(xiàn)第一行就定義了一條報錯:
assert type(data) is bytes, \ "write() argument must be a bytes instance"
對照上面的報錯信息,發(fā)現(xiàn)可能是變量data的類型,不是bytes,所以在handlers.py181行代碼self.write(data)上面加一句:
data=data.encode(),再次刷新程序,發(fā)現(xiàn)所有報錯居然都沒了,程序正常運(yùn)行。
以上這篇解決python3中自定義wsgi函數(shù),make_server函數(shù)報錯的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 詳解Python程序與服務(wù)器連接的WSGI接口
- Docker構(gòu)建python Flask+ nginx+uwsgi容器
- python 解決flask uwsgi 獲取不到全局變量的問題
- VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法詳解
- Python開發(fā)之Nginx+uWSGI+virtualenv多項目部署教程
- CentOS7部署Flask(Apache、mod_wsgi、Python36、venv)
- 詳解如何在Apache中運(yùn)行Python WSGI應(yīng)用
- Python WSGI的深入理解
- python Web開發(fā)你要理解的WSGI & uwsgi詳解
- Python模塊WSGI使用詳解
- 詳解python使用Nginx和uWSGI來運(yùn)行Python應(yīng)用
- 詳解使用Nginx和uWSGI配置Python的web項目的方法
- 淺析Python 中的 WSGI 接口和 WSGI 服務(wù)的運(yùn)行
相關(guān)文章
使用 Python 獲取 Linux 系統(tǒng)信息的代碼
在本文中,我們將會探索使用Python編程語言工具來檢索Linux系統(tǒng)各種信息,需要的朋友可以參考下2014-07-07
Python中PDF轉(zhuǎn)Word的多種實現(xiàn)方法
在日常辦公和數(shù)據(jù)處理中,經(jīng)常需要將PDF文檔轉(zhuǎn)換為Word文檔,以便進(jìn)行編輯、修改或格式調(diào)整,Python作為一種強(qiáng)大的編程語言,提供了多種庫和工具來實現(xiàn)這一功能,以下是對Python中PDF轉(zhuǎn)Word技術(shù)的詳細(xì)介紹,需要的朋友可以參考下2025-01-01
Pandas告警UserWarning:pandas?only?supports?SQLAlchemy?conn
這篇文章主要給大家介紹了關(guān)于Pandas告警UserWarning:pandas only supports SQLAlchemy connectable的處理方式,文中還分享了pandas還有哪些userwarning,對大家學(xué)習(xí)或者工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-02-02
Python Tkinter模塊實現(xiàn)時鐘功能應(yīng)用示例
這篇文章主要介紹了Python Tkinter模塊實現(xiàn)時鐘功能,結(jié)合實例形式分析了Tkinter模塊結(jié)合time模塊實現(xiàn)的時鐘圖形繪制與計時功能相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
Python 實現(xiàn)隨機(jī)數(shù)詳解及實例代碼
這篇文章主要介紹了Python 實現(xiàn)隨機(jī)數(shù)詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04
淺談在django中使用filter()(即對QuerySet操作)時踩的坑
這篇文章主要介紹了淺談在django中使用filter()(即對QuerySet操作)時踩的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03

