gpt4 book ai didi

python - Wsgiref 错误 : AttributeError: 'NoneType' object has no attribute 'split'

转载 作者:太空狗 更新时间:2023-10-29 18:23:11 25 4
gpt4 key购买 nike

我正在尝试实现我自己的 wsgiref 版本以用于学习目的,我最终在这里:

from wsgiref.simple_server import make_server

class DemoApp():
def __init__(self, environ, start_response):
self.environ = environ
self.start = start_response

def __iter__(self, status):
self.status = '200 OK'
response_headers = [('Content-type','text/plain')]
self.start(status, response_headers)
return ["Hello World"]


if __name__ == '__main__':
httpd = make_server('', 1000, DemoApp)
print("Serving on port 1000")
httpd.serve_forever()

当我转到端口 1000 时,出现属性错误。

AttributeError: 'NoneType' object has no attribute 'split'

我在哪里留下了错误?

堆栈跟踪:

Serving on port 1000
Traceback (most recent call last):
File "C:\Python27\lib\wsgiref\handlers.py", line 86, in run
self.finish_response()
File "C:\Python27\lib\wsgiref\handlers.py", line 131, in finish_response
self.close()
File "C:\Python27\lib\wsgiref\simple_server.py", line 33, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
127.0.0.1 - - [11/Jan/2014 12:40:09] "GET / HTTP/1.1" 500 59
Traceback (most recent call last):
File "C:\Python27\lib\SocketServer.py", line 295, in _handle_request_noblock
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 54469)
self.process_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python27\lib\SocketServer.py", line 649, in __init__
self.handle()
File "C:\Python27\lib\wsgiref\simple_server.py", line 124, in handle
handler.run(self.server.get_app())
File "C:\Python27\lib\wsgiref\handlers.py", line 92, in run
self.close()
File "C:\Python27\lib\wsgiref\simple_server.py", line 33, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'

最佳答案

您应该将返回的正文编码为 utf-8

return ["Hello World".encode("utf-8")]

这段代码对我来说工作正常,我使用的是 Python 3.3.3:

from wsgiref.simple_server import make_server


def app(env, start_response):
body = "Hello"
status = "200 OK"
headers = [("Content-Type", "text/plain; charset=utf-8")]
start_response(status, headers)
return [body.encode("utf-8")]

port = 9080
httpd = make_server("localhost", port, app)
print("Server started on port: ", port)
httpd.serve_forever()

关于python - Wsgiref 错误 : AttributeError: 'NoneType' object has no attribute 'split' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21059640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com