How the internet and the HTTP protocol work


Lesson 2 of 14 · Python for the web

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.

The internet works as a system of communication between clients (browsers) and servers.

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.

An example of client – server communication
Browser → HTTP request → Python server → HTTP response → Browser

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.

HTTP decides how requests are sent and how the server returns responses.

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
Status codes are very important in web programming because they show us whether a request was processed successfully.

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.

In the next lessons we will make our own Python web server using the Flask framework.

A real-life example

Imagine a web application for the weather forecast.

  1. The user opens the site in the browser
  2. The browser sends an HTTP request to the server
  3. The server processes the data
  4. The server returns an HTML page with the forecast
  5. 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.