Sitemap
Tohir Mirxomitov

Tohir Mirxomitov

Learner

Building Python Framework based on WSGI.

2 min readJul 25, 2026

Finished free youtube course “Building Your Own Framework in Python” and want to share my experience.

By answering following questions, we will learn some basic concepts:

  1. What is WSGI? (Web Server Gateway Interface)

WSGI is a specification that defines one standard interface between Python web applications and the servers that run them .

2. What is Web server? What is Python web application?

A web server is a program that speaks HTTP. Nginx, Apache, and Caddy are the common ones — regular applications that you install and start, just like any other software on the machine.

A Python web application is not a program in the usual sense. It has no entry point, doesn’t own a process, and doesn’t listen on a port. It is more like a library than an application: it is imported and called by something else, once per request.

3. What problem is solved by WSGI?

Get Tohir Mirxomitov’s stories in your inbox

Join Medium for free to get updates from this writer.

Nginx is written in C. It has no Python interpreter and no way to call a Python function — it cannot execute a .py file. So a third program sits in between: a WSGI server like Gunicorn or uWSGI, which owns the Python interpreter, imports your application once, and calls it for every request.

Press enter or click to view image in full size

© Image from: https://ruslanspivak.com/lsbaws-part2/

A Simple WSGI Application

The simplest possible WSGI application looks like this:

from wsgiref.simple_server import make_server

def application(environ, start_response):
response_body = b"Hello, world"
start_response('200 OK', [('Content-Type', 'text/html'), ('Content-Length', str(len(response_body)))])
return [response_body]

httpd = make_server('localhost', 8000, application)
httpd.handle_request()

What is happening here?
make_server creates a plain HTTP server bound to localhost:8000 and hands it our application function. From that point the server owns the socket, and our function just waits to be called.

When a request arrives, the server parses the raw HTTP bytes and calls application with two arguments. environ is a dictionary containing everything it parsed — method, path, headers, query string. start_response is a callback we invoke to set the status code and response headers.

While headers go out through start_response, the body is what we return — a list of byte strings, because HTTP responses are bytes, not text.

Used resources:

  1. https://medium.com/@uhasker/wsgi-explained-cc3ddc1b232e

2. https://ruslanspivak.com/lsbaws-part2/

--

--