Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
快速Python书籍消息_wall01.py_Python_Python 3.x - Fatal编程技术网

快速Python书籍消息_wall01.py

快速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

我想知道是否有人能帮我弄清楚为什么我总是得到错误断言错误:头名称/值必须是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):
    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']”使一切正常。非常感谢。