Quick Python Book message_wall01.py

时间:2015-03-27 01:06:27

标签: python python-3.x

我想知道是否有人可以帮我弄清楚为什么我一直收到错误AssertionError:标题名称/值必须是str类型(得到b'Content-type') 127.0.0.1 - - [26 / Mar / 2015 20:50:52]“GET /favicon.ico HTTP / 1.1”500 59.代码来自Manning出版的“The Quick Python Book”。

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()

1 个答案:

答案 0 :(得分:1)

从以下位置删除b

status = '200 OK' # HTTP Status
headers = [('Content-type', 'text/html; charset=utf-8')]

并添加到return ["<h1>Message Wall</h1>"]。您已阅读unicode/str并撰写bytes

旧文档有一个错误,其中的示例使用了b'200 OK'等等。这本书可能基于这本书,有一个旧的错误报告here。当前docs显示正确的用法。

一旦你这样做,你将看到http://localhost:8000/的输出并且没有错误。