最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決python3中自定義wsgi函數(shù),make_server函數(shù)報錯的問題

 更新時間:2017年11月21日 20:16:54   作者:5K小碼  
下面小編就為大家分享一篇解決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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • OpenCV灰度化之后圖片為綠色的解決

    OpenCV灰度化之后圖片為綠色的解決

    這篇文章主要介紹了OpenCV灰度化之后圖片為綠色的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Python生成不重復(fù)隨機(jī)值的方法

    Python生成不重復(fù)隨機(jī)值的方法

    這篇文章主要介紹了Python生成不重復(fù)隨機(jī)值的方法,實例分析了Python算法實現(xiàn)與Python自帶方法的實現(xiàn)技巧,非常簡單實用,需要的朋友可以參考下
    2015-05-05
  • 使用 Python 獲取 Linux 系統(tǒng)信息的代碼

    使用 Python 獲取 Linux 系統(tǒng)信息的代碼

    在本文中,我們將會探索使用Python編程語言工具來檢索Linux系統(tǒng)各種信息,需要的朋友可以參考下
    2014-07-07
  • Python中PDF轉(zhuǎn)Word的多種實現(xiàn)方法

    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
  • 在pycharm中顯示python畫的圖方法

    在pycharm中顯示python畫的圖方法

    今天小編就為大家分享一篇在pycharm中顯示python畫的圖方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Pandas告警UserWarning:pandas?only?supports?SQLAlchemy?connectable處理方式

    Pandas告警UserWarning:pandas?only?supports?SQLAlchemy?conn

    這篇文章主要給大家介紹了關(guān)于Pandas告警UserWarning:pandas only supports SQLAlchemy connectable的處理方式,文中還分享了pandas還有哪些userwarning,對大家學(xué)習(xí)或者工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-02-02
  • 簡單聊聊Python中多線程與類方法的交互

    簡單聊聊Python中多線程與類方法的交互

    在Python編程中,多線程是一種提高程序運(yùn)行效率的有效手段,本文將通過簡潔的語言、清晰的邏輯和實際的代碼案例,探討Python多線程如何調(diào)用類方法,感興趣的可以了解下
    2025-01-01
  • Python Tkinter模塊實現(xiàn)時鐘功能應(yīng)用示例

    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ù)詳解及實例代碼

    這篇文章主要介紹了Python 實現(xiàn)隨機(jī)數(shù)詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 淺談在django中使用filter()(即對QuerySet操作)時踩的坑

    淺談在django中使用filter()(即對QuerySet操作)時踩的坑

    這篇文章主要介紹了淺談在django中使用filter()(即對QuerySet操作)時踩的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03

最新評論

霞浦县| 杭锦旗| 瑞安市| 通辽市| 太谷县| 临城县| 达孜县| 乌拉特后旗| 革吉县| 龙南县| 剑川县| 瑞安市| 邓州市| 玛沁县| 绥芬河市| 射洪县| 双柏县| 图们市| 盐源县| 宁明县| 资阳市| 洛南县| 望奎县| 抚顺市| 宜兴市| 大同县| 金阳县| 合阳县| 常德市| 诸暨市| 台东市| 汝州市| 涟水县| 乌什县| 萍乡市| 九龙城区| 望谟县| 牡丹江市| 凤城市| 井冈山市| 浦江县|