Key Aspects of Web Development in Python

Theory: Python backend

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.