How the Web Works
When you open a website, at least two computers talk to each other: yours and a computer somewhere else that stores the site. Programs on both sides follow agreed rules so that a phone in Kochi can read a page stored on a server in another country in under a second.
In this lesson you will follow what happens when you type a web address, take a URL apart, read a real HTTP request and response, and see how JSON lets programs swap data. This is the background you need before you learn to build websites.
Clients and servers
- A client is your device and the program on it that uses the web, usually a browser such as Chrome or Firefox, or an app.
- A server is a computer that stores websites or data and sends them to clients when asked.
Think of a restaurant. You (the client) ask the waiter for a dish. The kitchen (the server) prepares it and sends it out. You never go into the kitchen; you only see what comes to your table.
The internet is the worldwide network of connected computers. The web is one service that runs on it: pages and files fetched by browsers. Email and video calls also use the internet, but they are not "the web".
What happens when you type an address
Suppose you type www.example.com into your browser and press Enter:
- DNS lookup. Computers find each other by number, called an IP address. The browser asks the DNS (Domain Name System), which works like a phone's contact list for the internet, for the IP address of
www.example.com. - Request. The browser sends an HTTP request to that address, asking for the page.
- Travel in packets. The data is split into small pieces called packets, which travel across the internet using the TCP/IP rules and are put back together at the other end.
- Response. If all is well, the server replies with "200 OK" and sends the page's files.
- Build the page. The browser reads the HTML, then requests the other files it mentions (CSS, JavaScript, images), and draws the page on your screen.
All of this usually takes less than a second.
The parts of a URL
A web address is called a URL (Uniform Resource Locator). Every part has a job:
| Part | Example | Meaning |
|---|---|---|
| Scheme | https | The rules to use; https is the secure version of HTTP |
| Domain | www.example.com | Which server to talk to |
| Path | /books/search | Which page or resource on that server |
| Query | ?city=Pune&page=2 | Extra information, as name=value pairs joined by & |
| Fragment | #results | A spot within the page; never sent to the server |
Both languages have tools to split a URL into its parts. In Python:
from urllib.parse import urlparse, parse_qs
url = "https://www.example.com/books/search?city=Pune&page=2#results"
parts = urlparse(url)
print("Scheme:", parts.scheme)
print("Domain:", parts.netloc)
print("Path:", parts.path)
print("Query:", parts.query)
print("Fragment:", parts.fragment)
print(parse_qs(parts.query))Scheme: https
Domain: www.example.com
Path: /books/search
Query: city=Pune&page=2
Fragment: results
{'city': ['Pune'], 'page': ['2']}And in JavaScript, with the built-in URL object:
const url = new URL("https://www.example.com/books/search?city=Pune&page=2#results");
console.log("Domain:", url.hostname);
console.log("Path:", url.pathname);
console.log("City:", url.searchParams.get("city"));
console.log("Page:", url.searchParams.get("page"));Domain: www.example.com Path: /books/search City: Pune Page: 2
HTTP requests and responses
HTTP (HyperText Transfer Protocol) is the language browsers and servers use. A request is plain text. This one asks for a page:
GET /books/search?city=Pune HTTP/1.1
Host: www.example.com
Accept: text/htmlThe first word is the method. GET means "please give me this". POST means "here is some data", used when you submit a form, such as a login or an order. The server answers with a response that starts with a status code:
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
<!DOCTYPE html>
<html lang="en">
...| Code | Meaning | When you see it |
|---|---|---|
| 200 | OK | Everything worked |
| 301 | Moved permanently | The page has a new address; the browser follows it |
| 404 | Not found | The page does not exist, or the URL has a typo |
| 500 | Server error | The program on the server crashed |
Codes starting with 4 mean the request had a problem; codes starting with 5 mean the server had a problem.
JSON: how programs swap data
Web pages are for people. When programs talk to each other, for example a weather app asking a server for today's forecast, they usually send data in JSON (JavaScript Object Notation). JSON looks almost exactly like a JavaScript object or a Python dictionary:
{
"city": "Jaipur",
"temperature": 31,
"rain": false,
"next_days": [33, 34, 30]
}JavaScript's fetch() sends a request and can read the JSON reply. This example asks a free practice server for a made-up user:
const response = await fetch("https://jsonplaceholder.typicode.com/users/1");
console.log("Status:", response.status);
const user = await response.json();
console.log("Name:", user.name);
console.log("City:", user.address.city);Status: 200 Name: Leanne Graham City: Gwenborough
await means "wait for the answer to arrive before going on", because data from the network takes a moment. A server address that gives data instead of pages is called an API (Application Programming Interface). You will learn to use APIs in Fetch API and Working with Web APIs.
Front end and back end
A website has two halves, and different languages work on each:
| Front end | Back end | |
|---|---|---|
| Runs on | The user's browser | The server |
| Job | What you see and click | Logins, saving orders, talking to the database |
| Languages | HTML, CSS, JavaScript | Python, JavaScript (Node.js), PHP, Java and others, plus SQL for databases |
| Tutorials here | HTML, CSS, JavaScript | Python, Node.js, PHP, SQL |
A programmer who works on both halves is often called a full-stack developer.
Common mistakes
- Thinking the internet and the web are the same. The web is one service on the internet.
- Leaving out the scheme. In code,
www.example.comon its own is not a full URL; writehttps://www.example.com. - Reading 404 as "the internet is down". It only means that one page was not found on a server that is working.
- Treating JSON as a JavaScript object. JSON arrives as text and must be parsed, with
response.json(),JSON.parse()or Python'sjson.loads().
Key points
- Clients (browsers and apps) request; servers respond.
- DNS turns a domain name into an IP address; data travels in packets using TCP/IP.
- A URL has a scheme, domain, path, query and fragment.
- HTTP requests use methods like
GETandPOST; responses carry status codes such as 200, 404 and 500. - Programs swap data as JSON, often through APIs.
- The front end runs in the browser; the back end runs on the server and talks to the database.
Exercise
Take the URL https://shop.example.in/search?q=cricket+bat&sort=price#top. Write a program that prints its domain, its path and the value of q. Do it in Python with urlparse() and parse_qs(), then in JavaScript with new URL(). What happens to the + in the search words?
Show solution
from urllib.parse import urlparse, parse_qs
parts = urlparse("https://shop.example.in/search?q=cricket+bat&sort=price#top")
query = parse_qs(parts.query)
print("Domain:", parts.netloc)
print("Path:", parts.path)
print("Search:", query["q"][0])Domain: shop.example.in Path: /search Search: cricket bat
const url = new URL("https://shop.example.in/search?q=cricket+bat&sort=price#top");
console.log("Domain:", url.hostname);
console.log("Path:", url.pathname);
console.log("Search:", url.searchParams.get("q"));Domain: shop.example.in Path: /search Search: cricket bat
In a query string, + stands for a space, so both languages decode cricket+bat to cricket bat. parse_qs() returns a list for each name, because a name can appear more than once, which is why the Python code takes item [0].
Quick Quiz
1. What does DNS do?
DNS works like a contact list, turning names such as www.example.com into IP addresses.
2. Which status code means "page not found"?
404 means the server is working but has no page at that address.
3. In https://www.example.com/books?city=Pune, what is /books?
The path says which page or resource on the server is wanted.
4. Where does front-end code run?
HTML, CSS and front-end JavaScript run in the browser; back-end code runs on the server.
Learn Intro to Programming with a mentor
Free lessons get you started. Our Python Programming training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
