SyntaxPilot
Courses/System Design/Lesson 2

System Design · Lesson 2 of 40 · 12 min

Client–server and HTTP

scheduled

These are the notes for System Design episode 2. Instead of memorising definitions, you invent client–server and HTTP by solving six problems, one at a time: one copy of the data with many people asking, two programs that can't understand each other, "did it work?", reading vs ordering, a server that forgets you, and a message anyone can read. Every Python demo from the video is below with its real output (Python 3.13, everything runs on your own machine on 127.0.0.1, no internet needed).

The six problems at a glance

Problem The fix you invent
One copy of the data, many people want it client and server
The two programs can't understand each other a protocol: HTTP
Did it work or not? status codes
Reading the menu vs placing an order methods and headers
The server forgets you after every request stateless design (plus cookies/tokens)
Anyone in between can read it HTTPS

Problem 1: one copy, many people → client and server

You made a course list on your laptop, courses.txt, and your hostel friends want it.

Attempt What goes wrong
It's on your laptop only you have it (1 copy)
Email it to everyone you fix a typo tomorrow and 50 people now have an old (stale) copy. Whose is right?
Now 10,000 friends 10,000 emails for every change: sending copies doesn't scale
Keep 1 copy, let them ask a program that always waits and answers whoever asks

The data is in one place and the people asking are many, so stop sending copies and start answering questions. This is a college canteen:

At the canteen On the web Meaning
Student (walks up and asks) Client the one who asks: a browser, an app, your own code
Counter (always open, always waiting) Server a program that keeps running and keeps listening
Order ("one samosa") Request what the client asks for
Plate Response what the server sends back

A request goes out, a response comes back. That is the client–server model.

Problem 2: they can't understand each other → a protocol

The server below sits on port 8001 and understands exactly one order: GIVE course. The client, written by your friend, asks in its own words.

import socket, threading

def server(sock):
    conn, _ = sock.accept()
    order = conn.recv(1024).decode()
    if order == "GIVE course":
        conn.send(b"Java Full Course")
    else:
        conn.send(b"??? I don't understand: " + order.encode())
    conn.close()

sock = socket.socket()
sock.bind(("127.0.0.1", 8001)); sock.listen()
threading.Thread(target=server, args=(sock,)).start()

client = socket.socket()
client.connect(("127.0.0.1", 8001))
client.send(b"send me the course please")
print("Server says:", client.recv(1024).decode())

Output:

Server says: ??? I don't understand: send me the course please

Both programs ran fine and the connection worked, yet they couldn't talk. Different people wrote them and nobody agreed on a common language. The internet has crores of clients and lakhs of servers; if each spoke its own language, nothing would work.

The printed order slip

The canteen solved this long ago with a printed order slip: everyone fills in the same format, so the counter can read every slip without thinking.

On the canteen slip In HTTP Example
What to do (eat here / parcel) Method GET, POST
Which item (item no. 12) Path /course
Extra details (less spicy, table 4) Headers Host, Content-Type
The parcel (what's inside) Body the data itself

A fixed set of rules like this is a protocol. The web's protocol is HTTP, the HyperText Transfer Protocol. Tim Berners-Lee created it at CERN, and the world's first website went live on 6 August 1991.

An HTTP request is just text

GET /course HTTP/1.1
Host: syntaxpilot.in
Accept: application/json

Part Example Meaning
Request line GET /course HTTP/1.1 method, path, version: "give me the course page"
Headers Host: syntaxpilot.in labelled details, one Name: value per line. Host says which website
Accept: application/json "I'd like JSON back"
Blank line (empty) "details over"
Body (optional) the data, if there is something to send

On the wire each line ends with \r\n (carriage return + newline), and the blank line is just \r\n on its own.

Speak HTTP by hand

Proof that HTTP is just text: the server uses Python's built-in HTTPServer with a do_GET handler, and the client uses no HTTP library at all. It types the request text by hand.

import socket, threading
from http.server import BaseHTTPRequestHandler, HTTPServer

class Shop(BaseHTTPRequestHandler):
    protocol_version = "HTTP/1.1"
    def do_GET(self):
        if self.path == "/course":
            body = b'{"name": "Java Full Course"}'
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)
        else:
            self.send_error(404)
    def log_message(self, *args): pass

srv = HTTPServer(("127.0.0.1", 8002), Shop)
threading.Thread(target=srv.serve_forever, daemon=True).start()

c = socket.create_connection(("127.0.0.1", 8002))
c.send(b"GET /course HTTP/1.1\r\n"
       b"Host: localhost\r\n"
       b"Connection: close\r\n"
       b"\r\n")
print(c.makefile("rb").read().decode())

Output (the Date line and Python version will differ on your machine):

HTTP/1.1 200 OK
Server: BaseHTTP/0.6 Python/3.13.17
Date: Tue, 27 Oct 2026 04:23:20 GMT
Content-Type: application/json
Content-Length: 28

{"name": "Java Full Course"}

This time the server understood. The response has the same shape as the request:

Request Response
request line: GET /course HTTP/1.1 status line: HTTP/1.1 200 OK
headers (Host, Connection) headers (Content-Type, Content-Length, Server, Date)
blank line blank line
body (optional) body: {"name": "Java Full Course"} (exactly 28 bytes)

Server and Date were added automatically by send_response. Connection: close asks the server to close the connection after answering, which is how our client knows the response is finished.

Problem 3: did it work? → status codes

If the server replied in English ("sorry, we don't have that"), every program would have to understand sentences, and every server would phrase them differently. The fix is a number.

The canteen counter says Class Examples
"Here you go" 2xx Success 200 OK, 201 Created
"Go to counter 2" 3xx Redirection 301 Moved Permanently, 302 Found
"Not on the menu" (your mistake) 4xx Client error 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found
"Gas cylinder is out" (the kitchen's problem) 5xx Server error 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable

The official HTTP rules, RFC 9110, define five classes; the fifth is 1xx informational ("wait, still working"). Just read the first digit to know whose fault it is.

Interview tip: 4xx → fix your request. 5xx → the server must fix something. That's the first question in any debugging.

A real 404 in code

This time the client uses Python's urllib, which writes the HTTP text for us. First the right path, then a typo: /cours without the e.

import threading, urllib.request
from http.server import BaseHTTPRequestHandler, HTTPServer

class Shop(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == "/course":
            self.send_response(200); self.end_headers()
            self.wfile.write(b"Java Full Course")
        else:
            self.send_error(404)
    def log_message(self, *args): pass

srv = HTTPServer(("127.0.0.1", 8003), Shop)
threading.Thread(target=srv.serve_forever, daemon=True).start()

r = urllib.request.urlopen("http://127.0.0.1:8003/course")
print(r.status, r.reason, "->", r.read().decode())
r = urllib.request.urlopen("http://127.0.0.1:8003/cours")

Output (the traceback is shortened; its last line is):

200 OK -> Java Full Course
Traceback (most recent call last):
  ...
urllib.error.HTTPError: HTTP Error 404: Not Found

The program didn't read any English sentence. It saw the number 404 and knew the client made the mistake. (urllib raises an exception for every 4xx and 5xx; you can catch it with except urllib.error.HTTPError as e: and read e.code.)

Problem 4: reading vs ordering → methods

On Swiggy you don't only read, you also order.

Action Do it 10 times Retry after the Wi-Fi drops
Read the menu nothing changes safe
Place an order 10 samosas arrive, and you pay 10 times a double order!

When the network drops, a browser or app may send the request again. That's safe for reading, not for ordering, so both sides must know up front which kind of request it is. The fix: the first word on the first line, the method.

Method Meaning Canteen / Swiggy example
GET read show me the menu (safe: nothing changes, can be retried freely)
POST create place a new order
PUT replace change my address to this
DELETE remove cancel the order
  • POST creates something new each time. That's why, if you refresh a page after submitting a form, Chrome asks you to Confirm Form Resubmission.
  • Do PUT or DELETE twice and the end result is the same. (The interview word for this is idempotent. GET, PUT and DELETE are idempotent; POST is not.)

Why headers exist: the parcel label

An order carries data too: "2 chai". How does the server know how long the body is and where it ends? A courier parcel has its details written on the label.

On a courier parcel HTTP header Tells the server
Weight: 2 kg Content-Length how many bytes are in the body
Contents: books (handle it right) Content-Type JSON? HTML? an image?
Deliver to: Flat 4B Host which website on this server (one server can host many websites)

Placing an order with POST

The server has a do_POST. It reads the Content-Length header so it knows how many bytes of body to read. The first attempt passes a normal string:

req = urllib.request.Request("http://127.0.0.1:8004/orders",
                             data="2 chai", method="POST")
r = urllib.request.urlopen(req)

Error (last line of the traceback):

TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.

Only bytes travel over the network, not text. Put a b in front of the string. The full working program:

import threading, urllib.request
from http.server import BaseHTTPRequestHandler, HTTPServer

orders = []
class Shop(BaseHTTPRequestHandler):
    def do_POST(self):
        size = int(self.headers["Content-Length"])
        orders.append(self.rfile.read(size).decode())
        self.send_response(201); self.end_headers()
        self.wfile.write(b"Order placed")
    def log_message(self, *args): pass

srv = HTTPServer(("127.0.0.1", 8004), Shop)
threading.Thread(target=srv.serve_forever, daemon=True).start()

req = urllib.request.Request("http://127.0.0.1:8004/orders",
                             data=b"2 chai", method="POST")
r = urllib.request.urlopen(req)
print(r.status, r.read().decode(), "| orders:", orders)

Output:

201 Order placed | orders: ['2 chai']

201 means Created: something new was made. GET read, POST created. (urllib added the Content-Length: 6 header for us, because b"2 chai" is 6 bytes.)

Problem 5: the server forgets you → stateless

RFC 9110 says HTTP is a stateless protocol: each request can be understood on its own. An HTTP server forgets you after every request. That's a feature, not a bug.

Canteen with 5 counters Stateless HTTP
Every slip is complete (item, table, paid) each request carries everything it needs
Any counter can serve you; no one needs to remember you any server can answer any request, so you can keep adding servers: that's the key to scaling
Show your ID card each time to prove who you are cookies / tokens are sent with every request

So how does Swiggy remember you're logged in? You "show your ID" every time: a cookie or a token travels with every request. That gets its own episode later.

Problem 6: anyone can read it → HTTPS

You log in to a website on café Wi-Fi and your password goes out. Remember how we typed HTTP by hand as plain text? Any router or Wi-Fi owner on the path can read it like a postcard (password=1234).

The fix: put the postcard in a sealed envelope. HTTPS is the same HTTP wrapped in TLS encryption. Same request and response, but nobody on the way can read them.

Year Share of Chrome navigations using HTTPS (per Google)
2015 about 30–45%
Since around 2020 about 95–99%
Chrome 154 (October 2026) Always Use Secure Connections on by default: Chrome warns before opening a public site without HTTPS

The whole chain

Client (asks) → Request (method · path · headers · body) → Server (waits, answers) → Response (status · headers · body)
You hit this problem You invented
1. One copy, many people client + server
2. No shared language HTTP
3. Did it work? status codes
4. Read vs order methods + headers
5. Scale stateless (+ cookies)
6. Privacy HTTPS

Key trade-offs

  • Stateless vs convenience: statelessness lets any server answer any request (easy scaling), but every request must carry its identity (cookies/tokens), which adds a little data and complexity.
  • Text protocol vs efficiency: HTTP/1.1 is human-readable text, which makes it easy to debug (we typed it by hand), at the cost of some extra bytes.
  • HTTPS vs plain HTTP: encryption adds a TLS handshake, but today it's the default for almost the whole web; privacy wins.
  • Retries vs duplicates: retrying a GET is safe; retrying a POST can create a duplicate order, so systems that retry must be careful with non-idempotent methods.

Common mistakes (real errors you'll hit)

Mistake Code Real error Fix
Wrong path urlopen("http://127.0.0.1:8003/cours") urllib.error.HTTPError: HTTP Error 404: Not Found a 4xx is the client's mistake: check the path, /course
Sending a str as POST data Request(url, data="2 chai", method="POST") TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str. data=b"2 chai" (or "2 chai".encode())
Starting a server on a busy port a second HTTPServer(("127.0.0.1", 8015), ...) while the first still runs OSError: [Errno 98] Address already in use stop the old server (or your previous run), or pick another port
Speaking your own format to a server client.send(b"send me the course please") no error: ??? I don't understand follow the protocol exactly
Using GET for something that changes data a "place order" link retries and refreshes repeat the action use POST to create, PUT to replace, DELETE to remove

The "Address already in use" error, reproduced:

from http.server import BaseHTTPRequestHandler, HTTPServer

first = HTTPServer(("127.0.0.1", 8015), BaseHTTPRequestHandler)
second = HTTPServer(("127.0.0.1", 8015), BaseHTTPRequestHandler)

Last line of the traceback:

OSError: [Errno 98] Address already in use

Practice

  1. Run the hand-typed HTTP demo and add an Accept: application/json header. What changes in the response?
  2. Make the server return 404 for /menu, then add a /menu route that returns Samosa, Chai, Maggi. Catch the 404 for /cours instead of crashing.
  3. Open your browser's DevTools Network tab and find one GET and one POST request.
  4. For each, say whose fault it is: 401, 502, 301, 403, 500.
  5. Why is it safe for a browser to retry a GET automatically, but not a POST?
Solutions and hints

1. Add one line to the request: b"Accept: application/json\r\n" before the blank line. The response stays exactly the same (status line, headers and the 28-byte JSON body), because our do_GET ignores the Accept header. A real server could use it to choose between JSON and HTML.

2. Add an elif before the else, and wrap the bad request in try:

import threading, urllib.request
from http.server import BaseHTTPRequestHandler, HTTPServer

class Shop(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == "/course":
            self.send_response(200); self.end_headers()
            self.wfile.write(b"Java Full Course")
        elif self.path == "/menu":
            self.send_response(200); self.end_headers()
            self.wfile.write(b"Samosa, Chai, Maggi")
        else:
            self.send_error(404)
    def log_message(self, *args): pass

srv = HTTPServer(("127.0.0.1", 8003), Shop)
threading.Thread(target=srv.serve_forever, daemon=True).start()

r = urllib.request.urlopen("http://127.0.0.1:8003/course")
print(r.status, r.reason, "->", r.read().decode())
r = urllib.request.urlopen("http://127.0.0.1:8003/menu")
print(r.status, r.reason, "->", r.read().decode())
try:
    urllib.request.urlopen("http://127.0.0.1:8003/cours")
except urllib.error.HTTPError as e:
    print(e.code, e.reason, "-> fix the path")

Output:

200 OK -> Java Full Course
200 OK -> Samosa, Chai, Maggi
404 Not Found -> fix the path

3. F12 → Network → reload a page: almost everything is GET. Log in or submit a search/comment form to see a POST. Click a row to see its method, status code and headers.

4. 401, 403: client (4xx: not logged in / not allowed). 502, 500: server (5xx). 301: neither, it's a redirection (3xx: "it moved, go here instead").

5. GET only reads, so repeating it changes nothing on the server. POST creates something each time, so a retry could create a duplicate order or payment.

Quick quiz

You order on an app, but the server's database is down. Which status code should the app most likely get?

  • A. 200 OK
  • B. 301 Moved Permanently
  • C. 404 Not Found
  • D. 503 Service Unavailable
Answer

D: 503 Service Unavailable. You did nothing wrong; the server can't handle the request right now, so it's a 5xx code.

Interview questions

  • Explain the client–server model with an example.
  • What are the parts of an HTTP request and an HTTP response?
  • What do the five status code classes mean? What's the difference between 401 and 403? Between 500 and 503?
  • What is the difference between GET and POST? Between PUT and POST?
  • What does idempotent mean? Which HTTP methods are idempotent?
  • What is the Content-Length header for? What is the Host header for?
  • What does "HTTP is stateless" mean? How do websites keep you logged in anyway?
  • What is the difference between HTTP and HTTPS?

Summary

  • Client asks, server waits and answers: one copy of the data, many people asking.
  • HTTP is an agreed text format: method · path · version, headers, a blank line, then the body. The response has the same shape: status line, headers, blank line, body.
  • Status codes: 2xx ok, 3xx look elsewhere, 4xx your mistake, 5xx the server's problem. The first digit tells you whose fault it is.
  • Methods: GET reads, POST creates, PUT replaces, DELETE removes. Headers like Content-Length, Content-Type and Host label the details.
  • HTTP is stateless: any server can answer any request, which is why it scales; cookies/tokens carry who you are.
  • HTTPS = HTTP inside TLS encryption, used by about 95–99% of Chrome page loads.

Next episode: a DNS deep dive. Today we assumed the client already knows the server's address. How does syntaxpilot.in become an IP address, in milliseconds, for the whole planet?