I’ve spun up web servers in the past before for various APIs and the like. I realized, though, that I wasn’t really able to pass the explain-to-your-grandma test.

There’s a couple of things we will cover

  1. networking fundamentals
  2. flask fundamentals
  3. that’s about it

Networking fundamentals

Networking protocols are the classic example of abstraction in computer science.

OSI model

The Open Systems Interconnection model provides a reference for how one ought to split a protocol stack. It is the Beckn to ONDC (if that doesn’t make sense, read about Beckn and ONDC). There’s 7 layers - each with an increasing level of abstraction.

We start with the physical layer - which does the transmission of raw bit streams over a physical medium like a wire or network switch or Ethernet hub. It converts digital bits that it gets from the data link into electrical, radio or optical signals. Stuff like voltage levels and modulation schemes and so on. Stuff from compression class.

Then there’s a bunch of layers I will blackbox for the sake of this exposition.

And finally we get to the application layer - where you are talking between applications. Literally ports on computers. Like port 5555 on IP 345.1435.154.51 and port 1234 on IP 235.325.352.15. Which might be a Postgres instance talking to a Flask instance.

Internet protocol suite

Good old DoD funded this protocol stack. The TCP/IP stack procided end-to-end data communication and specifies how data should be

  1. packetized
  2. addressed
  3. transmitted
  4. routed
  5. received

The abstraction layers are:

  1. link layer
  2. internet layer
  3. transport layer
  4. application layer

The application layer handles process-to-process data exchange for applications. It includes protocols used by most applicaitons for exchanging data - like SMTP or HTTP or FTP.

Web servers

We jump to web servers now. There’s servers and then there’s web servers. Examples of web servers are Apache and Nginx.

A web server is a program that understands and knows how to service HTTP requests (things like GET, POST and so on). There’s a bunch of features that web servers like Nginx often have:

  1. logging
  2. service static content i.e. interact with filesystem
  3. authorization
  4. caching
  5. mapping URLs cleaner URLs whatever that means
  6. custom error pages

I’m most interested in the ability of web servers to serve dynamic content

How web servers serve dynamic content

The issue at hand is people started to write custom code all the time for every single minute use case. Want to have your web server (progam that understands HTTP) call a program that returns the nearest pizza place? Write custom code from scratch. Want to call a random number generator? custom code from scratch.

CGI was a standard that was written to make this process more straightforward. Now you could pass along standard input params and have both parties understand things easily. The stdout from the callee was then just piped along by the HTTP web server to the client.

WSGI

WSGI (pronounced whiskey apparently) is a simple interface for webservers to forward requests to frameworks like Flask or Django (web apps/frameworks written in Python). Python web frameworks used to be written to understand CGI/FastCGI but then that led to problems like certain frameworks only supporting certain interfaces and by extension only certain webservers that implemented those interfaces.

Java’s model was to have a Java-specific gateway interface called the “servlet API”. The Python folks decided to do the same thing. It also means it is easier to switch between web servers and for that matter between web frameworks/application servers without having to rewrite a lot of your code.

WSGI actually does a bunch of other things as well now - things like load balancing and post-processing and multiprocessing and so on. Pretty cool stuff. There’s some neat WSGI examples on the Wikipedia page.

Flask

Okay, so where does Flask come into the picture?

Flask is a WSGI application. It understands WSGI and responds to WSGI “requests”.

Common gotcha: Running Flask in development mode means there is a very simple WSGI web server running in addition to your Flask WSGI web app. This is not what you want to use in prod.

The Flask docs are fantastic, and I will defer to that for the rest of the Flask exposition.