I have implemented a simple Flask-like framework using HTTPServer and BaseHTTPRequestHandler from http.server for educational purposes.
I have found out that I can replace HTTPServer with WSGIServer from wsgiref.simple_server and the code runs without any other modification.
The run method of my implementation works as follows:
def run(self, host: str, port: int):
web_server = WSGIServer((host, port), Request)
try:
print("Server is running at port", port)
web_server.serve_forever()
except KeyboardInterrupt:
web_server.server_close()
print("Server stopped")
When I read the gunicorn docs, I have found out that gunicorn expects a function like this:
def app(environ, start_response):
"""Simplest possible application object"""
data = b'Hello, World!\n'
status = '200 OK'
response_headers = [
('Content-type', 'text/plain'),
('Content-Length', str(len(data)))
]
start_response(status, response_headers)
return iter([data])
And that wsgiref.simple_server.make_server takes a function like the above and instantiates a WSGIServer and calls the serve_forever() method like I did in my implementation.
I have tried to return the app from thr WSGIServer from the WSGIServer.get_app() method but it returns None.
I would like to know how to run my WSGIServer class using gunicorn from the terminal.