Build a Simple Python WSGI Server that Runs Django, Flask, and Pyramid

This guide shows how to create a minimal Python WSGI server that can run Django, Flask, and Pyramid applications without modifying server or framework code, explains WSGI fundamentals, provides step‑by‑step setup, code snippets, testing commands, and demonstrates the server‑framework interaction.

ITPUB
ITPUB
ITPUB
Build a Simple Python WSGI Server that Runs Django, Flask, and Pyramid

When you choose a Python web framework, the framework often restricts which web server you can use, and vice‑versa. To run Django, Flask, and Pyramid applications on the same server without changing either the server or the framework code, you need the Python Web Server Gateway Interface (WSGI), which decouples server and framework.

WSGI defines a simple callable interface that all modern Python web frameworks implement on the framework side, while a server implements the corresponding server‑side interface. This separation lets you mix any WSGI‑compatible server (e.g., Gunicorn, uWSGI, Waitress) with any WSGI‑compatible framework.

The article provides a compact WSGI server implementation (about 150 lines) saved as webserver2.py. The server loads an application callable from a framework module and forwards HTTP requests to it.

First, create a virtual environment with virtualenv and install the three frameworks:

virtualenv venv
source venv/bin/activate
pip install pyramid flask django

Next, create a Pyramid application. Save the following code as pyramidapp.py in the same directory as webserver2.py (or download it from the referenced GitHub repository). Then start the server:

python webserver2.py pyramidapp

Open http://localhost:8888/hello in a browser or run curl http://localhost:8888/hello to see the response.

Repeat the same steps for a Flask application saved as flaskapp.py. After starting the server with flaskapp, the /hello endpoint returns the Flask‑generated output, which can also be verified with curl.

For Django, clone the repository and use the provided djangoapp.py, which adds a helloworld Django project to the Python path and imports its WSGI application. Run the server with djangoapp and test the /hello URL similarly.

The WSGI interaction works as follows:

The framework provides a callable named application (the WSGI entry point).

The server receives an HTTP request, creates an environ dictionary containing CGI/WSGI variables, and a start_response callable, then calls application(environ, start_response).

The framework generates an HTTP status line and response headers, passes them to start_response, and returns the response body.

The server combines the status, headers, and body into a full HTTP response and sends it back to the client.

When a request is processed, the server follows these steps:

Load the application callable from the framework.

Read the incoming HTTP request.

Parse the request.

Create the environ dictionary with required WSGI/CGI variables.

Call application(environ, start_response) and obtain the response body.

Combine the status, headers set via start_response, and body into an HTTP response.

Return the HTTP response to the client.

By following the steps above, you now have a functional WSGI server capable of running multiple Python web frameworks without any code changes to either the server or the frameworks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentDjangoFlaskWeb serverWSGIPyramid
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.