Skip to lesson

Clients and servers

A learner opens the ShopLite app and sees today's products. The app holds none of them: it must ask something else, and something else must answer. Who asks, who answers, and how?

The request-response cycle

A request is the message a client sends to a server; the response is the server's one reply. One request plus its response is one request-response cycle.

The first example sends one request with curl and prints the one response that comes back.

Example 1 · Your first request with curl

Terminal
curl https://example.com
Sample output
<!doctype html><html lang=en><head><meta charset=utf-8><link rel=icon href=data:,><meta name=viewport content="width=device-width,initial-scale=1"><title>Example Domain</title><style>html{color-scheme:light dark;background:light-dark(#eee,#222)}body{font:16px/1.6 system-ui,sans-serif;max-width:26em;margin:auto;padding:25vh 2em 2em;text-align:center}</style></head><body><p>This domain is for use in documentation examples without needing permission. This is not a service; avoid relying on it for testing and monitoring purposes.</p><script src=/s.js></script></body></html>

What this code does

  • curl sent one request to the server named example.com, and the server answered with one response: this HTML page. Then curl stopped. That is one full request-response cycle.
  • A browser would draw this HTML as a page that says "Example Domain". curl does not draw anything; it prints the raw text, which is exactly what travelled over the network.

The client always speaks first: a server never sends a page nobody asked for. If the request cannot reach the server, or the server is not running, there is no response and the client reports an error.

curl (say "curl", short for "client for URLs") is a client you run in the terminal. It sends one request, prints the response and stops, so you see the cycle with no browser in the way.

Order of execution

Order one request-response cycle

Tap the steps in the order they happen.

In interviews, "client" and "server" mean roles, not kinds of computers. Your laptop is a client when it opens a website and a server when it runs your own web app.

Browsers, apps and curl are all clients

A client is any program that sends requests to a server: a browser, a phone app, a script or curl. They all send the same kind of request and differ only in what they do with the response.

The second example asks curl to show the request it sends and the start of the response, so you can see the client name itself.

Example 2 · What curl says about itself

Terminal
curl -sv https://example.com -o example.html
Sample output (trimmed; ... marks lines left out)
* Host example.com:443 was resolved.
* IPv6: 2606:4700:10::ac42:93f3, 2606:4700:10::6814:179a
* IPv4: 104.20.23.154, 172.66.147.243
...
> GET / HTTP/1.1
> Host: example.com
> User-Agent: curl/8.21.0
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Date: Fri, 16 Oct 2026 02:23:00 GMT
< Content-Type: text/html; charset=utf-8
< Server: cloudflare
...
* Connection #0 to host example.com:443 left intact

What this code does

  • Lines starting with > are the request curl sent. User-Agent: curl/8.21.0 is curl naming itself; Chrome sends a long Mozilla/5.0 ... line instead.
  • Lines starting with < are the start of the response. 200 OK means the request worked; the page itself went into example.html because of -o.
  • The lines starting with * are curl's own notes about what it is doing. The next lessons explain each of them.

A browser draws the response as a page, then sends more requests for the pictures, styles and scripts the page needs. A phone app reads the response and shows it in its own screens; curl just prints it. Each client names itself in a line called User-Agent, so the server knows who is asking.

The -v option shows the request (lines starting with >) and the start of the response (lines starting with <). -s hides the progress bar and -o saves the page to a file. In Windows PowerShell type curl.exe, because plain curl there is a different command; everywhere else type curl.

One server, many clients; one machine, many servers

A server is a program that runs all the time and waits for requests, then sends one response to each. One computer can run many servers at once.

The next two examples start a tiny server on your own computer and send it requests, then list the servers waiting on this machine.

Example 3 · Run a tiny server and send it requests

Terminal
# Terminal 1: inside a folder named hostel-site that holds index.html and room.jsonpython -m http.server 8000# Terminal 2: two requests to that servercurl http://localhost:8000/curl http://localhost:8000/room.json
Sample output
# Terminal 1 (the server prints one line per request)
Serving HTTP on :: port 8000 (http://[::]:8000/) ...
::1 - - [16/Oct/2026 07:56:19] "GET / HTTP/1.1" 200 -
::1 - - [16/Oct/2026 07:56:19] "GET /room.json HTTP/1.1" 200 -

# Terminal 2
<h1>Hostel notice board</h1>
<p>Library timings: 7:30 to 9:30</p>
{"room": 214, "fee": 45000, "paid": true}

What this code does

  • Python 3 includes a tiny file server: python -m http.server 8000 serves the files of the current folder on port 8000 (on macOS type python3). The server keeps running and waiting until you press Ctrl+C.
  • Each curl command is one request, and the server logs one line for each: who asked (::1 is this computer), what was asked for (GET /room.json) and the result (200). Open http://localhost:8000 in a browser too and new lines appear; the server does not care which client asks.
  • Make index.html and room.json in any editor with the text shown in the output. The server shares every file in its folder, so run it only in a folder made for the demo.

Example 4 · Many servers waiting on one computer

Terminal
# Windows (Command Prompt, PowerShell or Git Bash)netstat -an | findstr LISTENING# macOSnetstat -an | grep LISTEN
Sample output (Windows, a few lines picked from a longer list)
  TCP    0.0.0.0:3306           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:5432           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:6379           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:8000           0.0.0.0:0              LISTENING
  TCP    [::]:3306              [::]:0                 LISTENING
  TCP    [::]:5432              [::]:0                 LISTENING
  TCP    [::]:6379              [::]:0                 LISTENING
  TCP    [::]:8000              [::]:0                 LISTENING

What this code does

  • netstat -an lists the network connections of this computer; the filter keeps only servers that are waiting (listening) for clients.
  • This one laptop runs four servers at once: MySQL on port 3306, PostgreSQL on 5432, Redis on 6379 and the Python demo server on 8000. Your list will be different.
  • Each port appears twice: once for the old style of address (0.0.0.0) and once for the new style ([::]). The next lesson explains both.

A server can answer many clients at the same time: a college results website gets thousands of requests in the first minute after results go live, and each request gets its own response.

Servers on one computer, for example a website, a database and a cache, share the machine but each waits on its own port, a number that tells incoming messages which server they are for. The next lesson explains ports fully.

A server that is not running cannot answer anyone. "The site is down" usually means the server program stopped or its machine is off.

Web pages versus APIs: HTML for people, JSON for programs

An API (Application Programming Interface) is a server address made for programs. It answers with plain data, usually JSON, instead of a page for people.

The next request goes to GitHub's API. Compare its answer with the HTML page in the first example.

Example 5 · An API answers with JSON

Terminal
curl https://api.github.com/users/octocat
Sample output (trimmed; numbers change over time)
{
  "login": "octocat",
  "id": 583231,
  "node_id": "MDQ6VXNlcjU4MzIzMQ==",
  "avatar_url": "https://avatars.githubusercontent.com/u/583231?v=4",
  "gravatar_id": "",
  "url": "https://api.github.com/users/octocat",
  "html_url": "https://github.com/octocat",
  ...
  "name": "The Octocat",
  "company": "@github",
  "location": "San Francisco",
  "public_repos": 8,
  "followers": 24615,
  "created_at": "2011-01-25T18:44:36Z",
  "updated_at": "2026-09-22T11:46:44Z"
}

What this code does

  • Compare this with Example 1. That response was HTML for a browser to draw; this one is JSON, plain names and values for a program to read.
  • The same profile is a web page at https://github.com/octocat (look at html_url). The website is for people; the API at api.github.com is for programs such as phone apps and scripts.

A web page is a response written in HTML, the language browsers turn into headings, text and buttons. JSON is a text format of names and values such as {"room": 214, "paid": true}; a phone app asks an API for it, then draws its own screen. A later module of this section covers JSON in detail.

Often one company runs both: github.com gives you web pages, and api.github.com gives the same information as JSON to programs.

localhost: client and server on the same machine

localhost is a special name that always means this same computer. A request to localhost never leaves your machine.

The last example calls the tiny server through localhost, then stops the server and calls again.

Example 6 · localhost with the server running, then stopped

Terminal
curl http://localhost:8000/room.json# now press Ctrl+C in the server's terminal, then run the same command againcurl http://localhost:8000/room.json
Sample output
{"room": 214, "fee": 45000, "paid": true}
curl: (7) Failed to connect to localhost:8000 after 2239 ms: Could not connect to server

What this code does

  • The first request reaches the server on this computer and gets the JSON back. The request never left the laptop: client and server both run here.
  • After Ctrl+C no program waits on port 8000, so the computer refuses the connection and curl prints error (7). There is no response at all, not even an error page. On macOS the message reads "Couldn't connect to server"; the meaning is the same.

Developers test a server they are building this way: the server and the client (browser or curl) run side by side on one laptop.

localhost on a friend's laptop means their laptop, not yours. A link like http://localhost:8000 works only on the computer where the server runs, and sending it on WhatsApp will not let anyone else open your server.

When the server is stopped, the same request fails at once with "Could not connect to server" (connection refused): nothing is waiting there to answer.

Next step

Check your understanding

12 quick questions