We’ll start by understanding how web servers work and the need for server gateway interfaces. Then, we’ll go over the list of Python web servers along with their salient features. Most of them are available as PyPI packages that you can install and start using in your projects. Let’s begin!

What is a Web Server?

When developing a Python application, you’ll use the development server to test your app. However, once the development is complete, you’ll have to deploy your app on a production-grade server. Amongst several layers of added functionality on top, a web server should have the basic functionality of an HTTP server. The Python web server should be able to handle HTTP requests (simple HTTP requests like GET, PUT, and POST) from clients, such as web browsers, and return the response. This response may include the HTTP status code indicating whether the request was processed successfully or not. It might consist of HTML web pages and semi-structured data such as JSON, XML, and more.

ASGI vs. WSGI: Understanding Server Gateway Interfaces

Suppose you’ve developed a Python application in a framework of your choice, say, Django or Flask. A conventional server may be unable to understand and process the requests directed to the Python application. Here’s where server gateway interfaces come into play. This gateway interface can be a Web Server Gateway Interface (WSGI) or an Asynchronous Server Gateway Interface (ASGI). The web server receives a request from a client, which it then redirects to the server gateway interface. The interface (WSGI/ASGI) interacts with the Python application through an exposed callable. The Web Server Gateway Interface (WSGI) handles requests sequentially. If there are multiple slow HTTP requests, then they’ll impact the throughput. Asynchronous Server Gateway Interface, or ASGI, is a successor to WSGI and has the additional ability to handle requests asynchronously. Suppose a currently serviced request requires a database read (that can potentially take a long time) and is waiting for the response. When the first request is waiting for a response, ASGI can handle a second incoming request. This is in contrast to WSGI, where the first request must be processed before the second request. Therefore, using ASGI may result in increased throughput. ASGI also lets you use asynchronous Python capabilities within your application.

Python Web Servers

Now, let’s list down the web servers you can use for your Python applications. We’ve also included certain frameworks that provide out-of-the-box support for production-ready servers. This is not an exhaustive list, and the web server recommendations below are not in any particular order.

Uvicorn

If you’re familiar with application development with FastAPI, you’ll have come across Uvicorn, a web server implementation. Uvicorn is an ASGI server implementation for Python. To install Uvicorn and associated dependencies using pip, run the following command: The following are Uvicorn’s salient features:

ASGI implementationSupport for running apps from an asynchronous Python environmentThe command-line tool with an extensive list of optionsSupports running applications from factory functionsProvides features to control server instances programmatically

Gunicorn

Gunicorn is a WSGI server for Python applications, well known for its advanced process management features. Uvicorn gives a Gunicorn worker class, allowing you to achieve performance and process management advantages simultaneously. This documentation page contains details on deploying Gunicorn. If you have Python 3.5 and later, you can install Gunicorn using pip: The following are Gunicorn’s salient features:

Simple and fastCompatible with many web frameworksSupport for use with an HTTP proxy serverAdvanced process management

CherryPy

CherryPy is a Python web framework—just like Django, Flask, and FastAPI—but not as popular as these frameworks. With several stable releases, CherryPy has been around and has evolved into a fully-fledged framework for over a decade. It also has a supportive developer community. You can check out the interesting Zen of CherryPy, which conveys the essence that CherryPy is a simple and minimalist yet powerful framework. CherryPy is a Python framework and not a web server, but it’s on our list as it natively supports a production-grade server—unlike most other frameworks. To install CherryPy and get started, run: The following are CherryPy’s salient features:

Pythonic framework using object-oriented Python; well suited for beginnersSupport for a production-ready HTTP server, CherootEasier to learn than other web frameworks, such as DjangoSupport for building and deploying simple web apps to microservices

Daphne

Django is one of the most popular Python web frameworks. If you’ve developed applications with Django, you’ll likely have used the Daphne web server. Daphne is one of the first ASGI server implementations used as a reference for ASGI server implementations. Daphne is available as a PyPI package that you can install using pip. Here’s a detailed tutorial that’ll walk you through building a full-stack production-ready Python application. The following are Daphne’s salient features:

Widely adopted in production environmentsReference for all ASGI server implementationsSupport for HTTP, HTTP2, and WebSocket protocols

Python Trio

The Trio project aims to provide production-grade asynchronous I/O capabilities: support for parallel I/O and concurrency. This project also has several web and HTTP Python libraries. You can install the latest release of Trio (June 2022) from PyPI. Here’s a list of useful Trio libraries for web programming with Python:

hypercorn: Hypercorn is an ASGI server that supports HTTPmuffin: Muffin is an ASGI web framework for Pythonstartlette: Starlette is a lightweight ASGI frameworktrio-websocket: support for WebSocket server and client

  • httpx: HTTP client for Python

    Twisted Web

    Twisted is a framework for Python applications. This event-driven framework includes a module for web programming called twisted.web. You can install Twisted and the associated modules using pip: The following are Twisted’s salient features:

    Developer-friendly features such as unit testing and static code checkingtwisted.web provides a WSGI server and support for HTTP clients and servers

    AIOHTTP

    AIOHTTP is a Python library that provides client and server-side capabilities through the client and server APIs. Here’s a useful video explaining the asynchronous programming capabilities on the client side to speed up requests to APIs significantly. The following are the salient features of AIOHTTP:

    Support for both the HTTP server and client functionalitySupport for logging to facilitate easier debugging; provides logging for a client, server, web sockets, and moreNative support for testing through the pytest Plugin for testing: pytest-aiohttp, Which is available as a PyPI package

    Tornado

    Tornado is another Python web framework that supports asynchronous networking. In recent years, there have been many stable releases of Tornado – the most recent version 6.2, was released in July 2022. Currently, Tornado supports Python 3.7 and later. Like most packages in this list, Tornado can also be installed using pip: The following are Tornado’s salient features:

    Support for HTTP server tornado.httpserver and clientScalable and uses non-blocking network I/OWell suited for applications that require connectivity for a substantially longer interval of timeUtilities for logging, unit testing, and moreSeamless integration with services for authentication

    Meinheld

    Meinheld is a WSGI server for Python. It uses and builds existing libraries for event handling and HTTP request processing. You can install Meinheld using pip: Some features of Meinheld:

    Uses http-parser library to handle incoming HTTP requestsBuilds on top of the picoev library for event handling

    Conclusion

    I hope you found this listicle of Python web servers helpful. Be sure to try them out in your future projects. We’ve listed the key features; to fully understand the dependencies and how they work, you’ll have to use them in your applications. 👩🏽‍💻 If you want to level up your Python skills, consider checking out Geekflare’s library of Python tutorials. Happy coding!

    9 Python Web Servers to Try for Your Next Project - 919 Python Web Servers to Try for Your Next Project - 869 Python Web Servers to Try for Your Next Project - 809 Python Web Servers to Try for Your Next Project - 999 Python Web Servers to Try for Your Next Project - 169 Python Web Servers to Try for Your Next Project - 459 Python Web Servers to Try for Your Next Project - 939 Python Web Servers to Try for Your Next Project - 389 Python Web Servers to Try for Your Next Project - 139 Python Web Servers to Try for Your Next Project - 79 Python Web Servers to Try for Your Next Project - 949 Python Web Servers to Try for Your Next Project - 279 Python Web Servers to Try for Your Next Project - 15