TCP, HTTP & SSE Explained — Build a Server From Scratch in Python

Опубликовано: 30 Июль 2026
на канале: AI System Design | Interview
19
5

Every time ChatGPT types out an answer word by word, three protocols are doing
the work: TCP, HTTP, and Server-Sent Events. In this video we build all three
from scratch in Python — standard library and Flask only — and watch the actual
packets on the wire with tcpdump.

No frameworks hand-waving it for you. By the end you'll know exactly what your
browser and the server are saying to each other, because you'll have written
the server yourself.

WHAT WE BUILD
A raw TCP echo server with the socket module — the same API under nginx,
Redis and Postgres
The bug that breaks it: head-of-line blocking, and the threaded fix
A Flask HTTP server, plus what actually happens when Content-Length lies
An SSE endpoint that streams live events to curl and to the browser's
EventSource — the same technique OpenAI, Anthropic and Ollama use

CHAPTERS
00:00 What's actually on the wire
00:45 Roadmap: TCP → HTTP → SSE
01:30 TCP: a phone call between two computers
02:30 The four-tuple: how one port serves thousands of clients
04:00 Writing a TCP server with zero libraries
06:00 Blocking vs busy-waiting (most people get this wrong)
07:00 Two clients, one thread — watch it break
08:00 Fixing it with threads
09:00 The 3-way handshake in tcpdump
10:00 HTTP is just text over TCP
12:00 Building it with Flask
14:00 Content-Length is a promise — breaking it on purpose
17:00 Why Flask closes every connection (and why nginx doesn't)
19:00 SSE: the HTTP response that never finishes
21:00 Streaming with a Python generator
23:00 Watching the stream in tcpdump
25:00 EventSource in the browser
26:00 Polling vs SSE vs WebSocket
27:00 What's next

WHY SSE AND NOT WEBSOCKETS
Every major LLM API streams over Server-Sent Events, not WebSockets. The model
only needs to push, and SSE rides on plain HTTP — so proxies, load balancers
and CDNs all just work.
#softwareengineering #python #networking #tcp #http #sse #backend #flask
#systemdesign #chatgpt