Register to get access to free programming courses with interactive exercises

Python backend Key Aspects of Web Development in Python

In this lesson, you'll learn what components make up the backend when written in Python. We'll get acquainted with the key terms we'll need for the course.

Web servers

Most web applications rely on the client-server model:

  • Client: The user opens the browser and sends a request
  • Server: The request from the browser goes to the web server, which begins to process it

Some languages embed the web server directly into the application, but most interpreted languages use an external program. There are several standalone web servers, but the most popular is Nginx. It handles incoming requests, serves static files, and distributes requests to web applications.

WSGI

If the web application backend runs on Python, we usually find a WSGI server running WSGI applications behind the web server. Let's take a closer look at what that is.

The WSGI (Web Server Gateway Interface) is the abstraction under which the requests get answered. It is a Python function that takes a request and returns a response. The basic WSGI application looks like this:

# The code below may seem incomprehensible, but that's not important
# The most important thing is that we can see that this is just a function

def app(environ, start_response):
    data = b"Hello, World!\n"
    start_response("200 OK", [
        ("Content-Type", "text/plain"),
        ("Content-Length", str(len(data)))
    ])
    return iter([data])

Let's take a closer look at this snippet of code:

  1. Everything about a particular query comes in the environ argument
  2. The `start_response' function sets the response parameters — its size and content type
  3. The function returns an iterator that returns the response line by line

The web application is easy to create:

  • Use the popular WSGI server gunicorn
  • Put the function in the example.py file
  • Run the command gunicorn -w 4 example:app

Web Framework

Above, we saw a simple web application. Even though it works, it'll return identical text for every query. Writing something more complex in this style would be problematic, though doable. To make a backend developer's life easier and to help them implement typical applications, we use frameworks — libraries that define a ready-made application structure.

The developer only needs to insert his code snippets into this structure, and the application structure is ready to use. The most popular web frameworks for Python are Django and Flask.

Web frameworks can do many different things:

  • Perform routing
  • Simplify work with headers and query data
  • Form responses in various formats
  • Store query histories in files for statistics and debugging

ORM and Templating Tool

What else is in the backend besides query processing? Most of the time, there are two tools:

  • ORM (Object-Relational Mapping) is a tool for working with records in databases. It represents records as objects that are understandable by Python
  • Templating is a tool that allows you to write HTML and CSS in separate files and modify their contents. It allows you to create a layout once and then programmatically retrieve different pages from the layout

Django and some other web frameworks already include an ORM and templating engine.


Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY
Suggested learning programs
profession
new
Developing web applications with Django
10 months
from scratch
under development
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.