快速Python书籍消息_wall01.py
我想知道是否有人能帮我弄清楚为什么我总是得到错误断言错误:头名称/值必须是str类型(得到b'Content-type') 127.0.0.1--[26/Mar/2015 20:50:52]“GET/favicon.ico HTTP/1.1”500 59。代码来自曼宁出版社的《快速Python手册》快速Python书籍消息_wall01.py,python,python-3.x,Python,Python 3.x,我想知道是否有人能帮我弄清楚为什么我总是得到错误断言错误:头名称/值必须是str类型(得到b'Content-type') 127.0.0.1--[26/Mar/2015 20:50:52]“GET/favicon.ico HTTP/1.1”500 59。代码来自曼宁出版社的《快速Python手册》 from wsgiref.simple_server import make_server def message_wall_app(environ, start_response): s
from wsgiref.simple_server import make_server
def message_wall_app(environ, start_response):
status = b'200 OK' # HTTP Status
headers = [(b'Content-type', b'text/html; charset=utf-8')]
start_response(status, headers)
# The returned object is going to be printed
return ["<h1>Message Wall</h1>"]
httpd = make_server('', 8000, message_wall_app)
print("Serving on port 8000...")
# Serve until process is killed
httpd.serve_forever()
从wsgiref.simple\u服务器导入make\u服务器
def消息\u墙\u应用程序(环境,启动\u响应):
状态=b'200 OK'#HTTP状态
headers=[(b'Content-type',b'text/html;charset=utf-8')]
启动\u响应(状态、标题)
#将打印返回的对象
返回[“消息墙”]
httpd=make_服务器(“”,8000,消息_墙_应用程序)
打印(“在端口8000上服务…”)
#服务,直到进程被终止
httpd.永远为你服务()
从以下位置移除b:
status = '200 OK' # HTTP Status
headers = [('Content-type', 'text/html; charset=utf-8')]
并添加到return[“消息墙”]
。您可以读取unicode/str
并写入字节
旧文档有一个bug,其中示例使用了b'200ok'
等。。这本书可能就是基于这个,有一个旧的bug报告。电流表显示正确的用法
完成此操作后,您将在http://localhost:8000/
没有错误。谢谢。从status和headers中删除b并将其添加到return语句中,使其成为“return[b'messagewall']”使一切正常。非常感谢。