Python中关于WSGI的一个问题请教

我在 IDE 中输入如下代码运行,然后在浏览器中输入 http://127.0.0.1:8051/,却显示 ”该网页无法正常运作 127.0.0.1 未发送任何数据。”。
from wsgiref.simple_server import make_server

def application(environ, start_response):
path = environ.get(‘PATH_INFO’)
if path == ‘/’:
response_body = “Index”
else:
response_body = “Hello”
status = “200 OK”
response_headers = [(“Content-Length”, str(len(response_body)))]
start_response(status, response_headers)
return [response_body]

httpd = make_server(‘127.0.0.1’, 8051, application)

httpd.serve_forever()
Python中关于WSGI的一个问题请教


7 回复

我帮 lz 测试了一下,linux 命令行 python2.7 下,ok 的……代码没问题


我无法理解你的问题

我是在 3.6 运行的。代码没报错。但是打开浏览器输入 URL,没有产生预期内容。

这个问题在于 response_body 必须是 byte 不是 str

嗯,感谢!已经解决!查了一下文档确实如此!
嗯,感谢!查了一下文档解决了,感觉自己还是文档不太熟。

在 2.7 的情况下,response_body 使用 str ;在 3.6 的情况下,必须是 byte.

回到顶部