解決python3中自定義wsgi函數(shù),make_server函數(shù)報(bào)錯的問題
#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)行,就開始報(bào)錯,報(bào)錯內(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)錯,一下就蒙圈了,各種google百度,各種查,google到時能查到一些,
但是英文不好,也看不太明白,百度查到的都是垃圾,根本就沒用,最后硬著頭皮,一點(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)錯誤,再按住ctrl點(diǎn)擊write方法,找到定義write方法的位置,發(fā)現(xiàn)第一行就定義了一條報(bào)錯:
assert type(data) is bytes, \ "write() argument must be a bytes instance"
對照上面的報(bào)錯信息,發(fā)現(xiàn)可能是變量data的類型,不是bytes,所以在handlers.py181行代碼self.write(data)上面加一句:
data=data.encode(),再次刷新程序,發(fā)現(xiàn)所有報(bào)錯居然都沒了,程序正常運(yùn)行。
以上這篇解決python3中自定義wsgi函數(shù),make_server函數(shù)報(bào)錯的問題就是小編分享給大家的全部內(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多項(xiàng)目部署教程
- 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項(xiàng)目的方法
- 淺析Python 中的 WSGI 接口和 WSGI 服務(wù)的運(yùn)行
相關(guān)文章
Python大數(shù)據(jù)之網(wǎng)絡(luò)爬蟲的post請求、get請求區(qū)別實(shí)例分析
這篇文章主要介紹了Python大數(shù)據(jù)之網(wǎng)絡(luò)爬蟲的post請求、get請求區(qū)別,結(jié)合具體實(shí)例形式分析了Python網(wǎng)頁爬蟲post請求與get請求相關(guān)使用技巧,需要的朋友可以參考下2019-11-11淺析python 內(nèi)置字符串處理函數(shù)的使用方法
這篇文章主要介紹了python 內(nèi)置字符串處理函數(shù)的使用方法,需要的朋友可以參考下2014-06-06python生成多個只含0,1元素的隨機(jī)數(shù)組或列表的實(shí)例
今天小編就為大家分享一篇python生成多個只含0,1元素的隨機(jī)數(shù)組或列表的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11使用Python進(jìn)行數(shù)據(jù)清洗和預(yù)處理的實(shí)現(xiàn)代碼
Python作為數(shù)據(jù)科學(xué)領(lǐng)域的熱門編程語言,提供了豐富的庫和工具來處理和清洗數(shù)據(jù),本文將介紹如何使用Python進(jìn)行數(shù)據(jù)清洗和預(yù)處理,并提供相應(yīng)的代碼示例,需要的朋友可以參考下2024-05-05Python實(shí)戰(zhàn)案例之可增刪改查的員工管理系統(tǒng)
本實(shí)戰(zhàn)案例主要對MySQL進(jìn)行一些基本操作,通過完成本實(shí)驗(yàn)任務(wù),要求學(xué)生熟練掌握MySQL的增刪改查,并對MySQL的增刪改查基本操作進(jìn)行管理員工的信息,需要的朋友跟著小編往下看吧2021-10-10