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

解決python3中自定義wsgi函數(shù),make_server函數(shù)報(bào)錯(cuò)的問(wèn)題

 更新時(shí)間:2017年11月21日 20:16:54   作者:5K小碼  
下面小編就為大家分享一篇解決python3中自定義wsgi函數(shù),make_server函數(shù)報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
#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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

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

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

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

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

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

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

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

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

    在pycharm中顯示python畫(huà)的圖方法

    今天小編就為大家分享一篇在pycharm中顯示python畫(huà)的圖方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    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,對(duì)大家學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-02-02
  • 簡(jiǎn)單聊聊Python中多線(xiàn)程與類(lèi)方法的交互

    簡(jiǎn)單聊聊Python中多線(xiàn)程與類(lèi)方法的交互

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

    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í)例代碼

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

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

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

最新評(píng)論

章丘市| 海南省| 岑巩县| 武威市| 辽源市| 蕲春县| 郯城县| 莱芜市| 九江市| 黄石市| 金寨县| 韶山市| 新野县| 南汇区| 和田市| 普定县| 龙泉市| 杭州市| 镇巴县| 陆川县| 扶余县| 雅安市| 长海县| 揭阳市| 丰原市| 尚义县| 平和县| 渝北区| 东港市| 浦城县| 定南县| 上虞市| 湖口县| 五大连池市| 鲁甸县| 呼和浩特市| 万源市| 泽库县| 汾阳市| 乐都县| 靖边县|