Tag Archives: wsgiref make_server Error

[Solved] wsgiref make_server Error: 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()

An error is reported when accessing port 8000. The coding problem is modified in the

code

return [response_body]  
=>Modfied
return [response_body.encode('utf-8')]

Normal operation