Python's cgi module has been deprecated since version 3.11. Even pouplar web servers still support CGI standard, without an official lib/module, writing CGI apps is tedious.
For Python programmers who want to stick with standard library and don't like high level web frameworks, it's time to migrate CGI apps to WSGI apps.
While there are tons of different implementations of WSGI standard, we're focus on three of them.
1 for development env
Python's standard library includes "wsgiref", a reference WSGI implementation.
from wsgiref.simple_server import make_server
def hello_world_app(environ, start_response):
status = "200 OK" # HTTP Status
headers = [("Content-type", "text/plain; charset=utf-8")] # HTTP Headers
start_response(status, headers)
# The returned object is going to be printed
return [b"Hello World"]
with make_server("", 8000, hello_world_app) as httpd:
print("Serving on port 8000...")
# Serve until process is killed
httpd.serve_forever()
2 for production env
uWSGI and Gunicorn are two of the most popular WSGI implementations for production env. The common design is to use Nginx as reverse proxy.uWSGI uses "uwsgi" protocol for communication with Nginx.
uwsgi_pass uwsgicluster;
Gunicorn uses "HTTP" protocol directly for communication with Nginx.
proxy_pass gunicorncluster;
No comments:
Post a Comment