Wsgiref Error: AttributeError: 'NoneType' object has no attribute 'split'
DemoApp
is called; The return value of DemoApp.__init__
is used.
DemoApp.__init__
returns nothing (You can't return anything in constructor).
Try following instead of DemoApp
class:
def DemoApp(environ, start_response):
response_headers = [('Content-type','text/plain')]
start_response('200 OK', response_headers)
return ["Hello World"]
Using class (Use __call__
instead of __iter__
):
from wsgiref.simple_server import make_server
class DemoApp:
def __call__(self, environ, start_response):
response_headers = [('Content-type','text/plain')]
start_response('200 OK', response_headers)
return ["Hello World"]
if __name__ == '__main__':
httpd = make_server('', 1000, DemoApp()) # Don't forget instantiate a class.
# ^^
print("Serving on port 1000")
httpd.serve_forever()
You should encode the returned body to utf-8
return ["Hello World".encode("utf-8")]
This code works fine with me, I am using Python 3.3.3:
from wsgiref.simple_server import make_server
def app(env, start_response):
body = "Hello"
status = "200 OK"
headers = [("Content-Type", "text/plain; charset=utf-8")]
start_response(status, headers)
return [body.encode("utf-8")]
port = 9080
httpd = make_server("localhost", port, app)
print("Server started on port: ", port)
httpd.serve_forever()
How about this, you need to yield
the output than returning it.
from wsgiref.simple_server import make_server
class DemoApp:
def __init__(self, environ, start_response):
self.environ = environ
self.start = start_response
def __iter__(self):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
self.start(status, response_headers)
yield 'Hello world!'
if __name__ == '__main__':
httpd = make_server('', 1000, DemoApp)
print("Serving HTTP on port 1000...")
httpd.serve_forever()