What Is a Web Server?
The Post Office of the Internet
Every time you visit a website, your browser sends a letter to a computer called a server. The letter says: "Please send me the page at this address." The server reads the letter, finds the right page, and sends back a reply.
That's a web server -- a post office that receives letters (requests) and sends back replies (responses).
Your Browser Web Server
│ │
├── "GET /about" ──────────────►│
│ (the request) │
│ ├── Find the right page
│ │
│◄─────────── "<html>..." ──────┤
│ (the response) │
│ │
What's in a Request?
Every request has three parts, just like a real letter:
- The method -- what you want to do (GET = "show me", POST = "here's some data")
- The path -- which page you want (
/about,/api/scores) - The headers -- extra info (what language you speak, what browser you use)
What's in a Response?
The server's reply also has three parts:
- The status code -- did it work? (200 = yes, 404 = page not found)
- The headers -- info about the reply (content type, length)
- The body -- the actual content (HTML, JSON, an image)
Our Building Blocks
| Concept | Analogy | What It Does |
|---|---|---|
| Request | The incoming letter | Parsed from raw HTTP text |
| Response | The reply letter | Built and sent back |
| Router | The mailroom | Matches paths to handlers |
| Handler | The person who writes the reply | A function that builds a response |
| Template | A form letter with blanks | HTML with {{ name }} placeholders |
| Server | The post office | Listens for connections |
Install
Or get the whole series at once with pip install fromscratch-pystack. To read the code or run the tests, clone it from GitHub.
Let's Start!
Head to Requests to learn how HTTP requests work.
Part 2: A Busy Restaurant
One waiter serving one table at a time gets slow. In Part 2 the server becomes a busy restaurant: one shopkeeper (PyNet's event loop) looks after many customers at once. Read these in order:
- Async Server -- opening the restaurant
- Keep-Alive -- customers who stay for more than one order
- Limits and Timeouts -- polite rules for everyone
- Handlers and Threads -- the slow kitchen job
- Middleware -- the doorman
- Testing Apps -- practising without customers
- Server-Sent Events -- the radio station
- WebSockets in PyWeb -- the phone line
- Escaping -- keeping tricks out of the page
- Caching -- remembering the answer
- Passwords -- keeping secrets secret
- Cookies and Sessions -- the wristband
- CSRF -- the forged permission slip
- Roles -- backstage passes
Try the runnable programs in examples/: async_hello.py,
sse_clock.py, ws_echo_page.py and login_demo.py.