wsgiref make_server 报错AssertionError: write() argument must be a bytes instance

from wsgiref.simple_server import make_server


def application(env, start_response):
    response_body = ["%s: %s" % (key, value) for key, value \
    in sorted(env.items())]
    response_body = '\n'.join(response_body)
    status = "200 ok"
    response_head = [("ContextType", "text/plain"), ("ContextLength",str(len(response_body)))]

    start_response(status, response_head)
    return [response_body]

httpd = make_server(
    "localhost",
    8000,
    application
)

httpd.handle_request()

访问8000端口出现报错,编码问题修改
wsgiref make_server 报错AssertionError: write() argument must be a bytes instance_第1张图片
代码中

return [response_body]  
=>修改为
return [response_body.encode('utf-8')]

运行正常

你可能感兴趣的:(python,网络协议)