From URL to page · Computer Networks
What happens after you type a URL and press Enter?
Course contentsWhy you need it
It's the classic interview question because the answer touches everything in this course: URLs, DNS, TCP, TLS, HTTP, caching and the browser. Being able to tell the story in order, and say where each kind of failure would show up, shows you understand how the web fits together.
The intuition
For https://shop.example.com/cart?id=7:
- Parse the URL: scheme
https, hostshop.example.com, port 443 (the default for https), path/cart, queryid=7. - DNS: look up the host's address (browser cache, OS cache, then a resolver).
- TCP: three-way handshake to address:443.
- TLS: agree on encryption keys and check the server's certificate is valid for that name.
- HTTP: send
GET /cart?id=7with headers (cookies too); get a status, headers and HTML. - Render: the browser parses HTML, fetches CSS, JavaScript and images (more requests, often reused connections), and draws the page.
Caches can skip steps: a cached DNS answer skips 2, a cached file skips 3–5.
Definition
URL
scheme://host:port/path?query#fragment. The fragment (#…) never leaves the browser.
TLS certificate
A file signed by a trusted authority saying "this public key belongs to shop.example.com". Browsers refuse the connection if it is expired or for another name.
Examples
Take a URL apart
Try it yourselfurllib.parse splits a URL into the pieces each step needs.
from urllib.parse import urlsplit
u = urlsplit("https://shop.example.com/cart?id=7#reviews")
port = u.port or {"https": 443, "http": 80}[u.scheme]
print(u.scheme, u.hostname, port)
print(u.path, u.query, u.fragment)- L4No port written, so use the scheme's default.
- L5What DNS and TCP need.
https shop.example.com 443 - L6The fragment stays in the browser.
/cart id=7 reviews
The journey as a list
Try it yourselfA function that describes the steps for any URL, skipping DNS when the answer is cached.
from urllib.parse import urlsplit
def journey(url, dns_cache):
u = urlsplit(url)
port = u.port or {"https": 443, "http": 80}[u.scheme]
steps = []
if u.hostname not in dns_cache:
steps.append("DNS " + u.hostname)
steps.append(f"TCP :{port}")
if u.scheme == "https":
steps.append("TLS")
steps.append("GET " + (u.path or "/"))
return steps
print(journey("https://shop.example.com/cart", set()))
print(journey("http://shop.example.com", {"shop.example.com"}))- L7A cached answer skips the lookup.
- L10Only HTTPS adds TLS.
- L15Cached DNS, plain HTTP on port 80, path defaults to /.
['TCP :80', 'GET /']
Common mistakes
http:// for anything private
Without TLS, anyone on the path (café Wi-Fi, a router) can read and change the traffic, cookies and passwords included.
Forgetting the extra requests
One page load is often dozens of requests (CSS, scripts, images, APIs). A slow page is usually slow there, not in the first HTML.
Quick quiz
1. The browser says the certificate is for another name. Which step failed?
Show the answer
Answer: A
Checking the certificate is part of the TLS handshake, before any HTTP is sent.
2. What does this print?
from urllib.parse import urlsplit
u = urlsplit("http://localhost:8000/api/items?sort=price")
print(u.hostname, u.port, u.path)Show the answer
Answer: A
The port is written explicitly; the query is separate from the path.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictDefault ports
Type exactly what this program prints.
- ModifyDon't forget TLS
steps(url)lists the network steps for a URL, but it never includes TLS and always uses port 80. Fix it: HTTPS uses port 443 and adds"TLS"after TCP. A port written in the URL wins over the default. - WriteWhere did it fail?
Write
diagnose(error)that maps an error message to the step that failed:- contains
Could not resolve→"DNS" - contains
Connection refusedortimed out→"TCP" - contains
certificate→"TLS" - starts with
HTTP/→ the status code's class:"HTTP 4xx"or"HTTP 5xx"(e.g. forHTTP/1.1 503 Service Unavailable) - anything else →
"unknown"
- contains
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| URL | 网址 | scheme://host:port/path?query#fragment. |
| TLS | 传输层安全 | Encryption and server identity checks for a connection; the S in HTTPS. |
| Certificate | 证书 | A signed statement that a public key belongs to a domain name. |