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

