http from scratch
Frameworks hide the best part. HTTP is just text over a socket — so let us read it ourselves.
a server in 15 lines
import socket
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("localhost", 8080))
s.listen()
while True:
conn, _ = s.accept()
request = conn.recv(4096).decode()
print(request.splitlines()[0]) # e.g. GET / HTTP/1.1
conn.sendall(
b"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nhello from a socket"
)
conn.close()
what you just learned
Request line, headers, blank line, body. That is the whole protocol. Everything a framework does — routing, middleware, JSON — is layered on top of exactly this.
Next up: parsing headers properly and speaking just enough HTTP/1.1 to survive a real browser.