How the internet and the HTTP protocol work
To understand web programming in Python, we first need to understand how communication works between the browser and the server.
When you open a web page, your browser sends a request to the server, and the server returns a response that the browser shows to the user.
What is a client?
A client is a program that sends requests to a server.
The most common client is a web browser:
- Chrome
- Firefox
- Edge
- Safari
When the user types an address into the browser, the browser sends a request to the server.
What is a server?
A server is a computer or a program that receives requests and returns a response.
In Python web programming the server is an application written in Python.
The server can send different kinds of content:
- an HTML page
- JSON data
- images
- video
What is the HTTP protocol?
HTTP (HyperText Transfer Protocol) is the protocol that defines how the client and the server communicate.
Every time you open a web page, the browser sends an HTTP request to the server.
An example of an HTTP request
When we open a page in the browser, the request can look like this:
GET /index.html HTTP/1.1
Host: example.com
This request means:
- GET – we are asking for data from the server
- /index.html – we are asking for a particular page
- Host – the server's domain
The server's HTTP response
The server then returns a response that contains an HTML page.
HTTP/1.1 200 OK
Content-Type: text/html
<html>
<h1>Hello from the server</h1>
</html>
The most important part of the response is the status code.
- 200 – a successful response
- 404 – the page does not exist
- 500 – an error on the server
How does Python use HTTP?
Python web frameworks such as Flask and Django handle HTTP requests automatically.
The programmer defines what the server should do when the user visits a certain address.
@app.route("/")
def home():
return "Welcome to my site"
When the user opens the home page, the Python server returns text or an HTML page.
A real-life example
Imagine a web application for the weather forecast.
- The user opens the site in the browser
- The browser sends an HTTP request to the server
- The server processes the data
- The server returns an HTML page with the forecast
- The browser shows the result
Related lessons
Frequently asked questions
What is HTTP?
HTTP is a protocol that makes communication possible between web browsers and servers.
What is the difference between a client and a server?
The client sends a request, and the server processes the request and returns a response.
What does the status code 404 mean?
It means that the server cannot find the requested web page.
Can Python handle HTTP requests?
Yes. Python frameworks such as Flask and Django make it possible to build web servers that handle HTTP requests.
