In this listicle, we present several Python web servers you can use to deploy your Python applications.
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.
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.
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:
$ pip install uvicorn[standard]
# includes installation of optional and Cython-based dependencies
The following are Uvicorn’s salient features:
Support for running apps from an asynchronous Python environment
The command-line tool with an extensive list of options
Supports running applications from factory functions
Provides features to control server instances programmatically
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:
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:
$ pip install CherryPy
The following are CherryPy’s salient features:
Pythonic framework using object-oriented Python; well suited for beginners
Support for a production-ready HTTP server, Cheroot
Easier to learn than other web frameworks, such as Django
Support for building and deploying simple web apps to microservices
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.
$ pip install daphne
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 environments
Reference for all ASGI server implementations
Support for HTTP, HTTP2, and WebSocket protocols
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.
$ pip install trio
Here’s a list of useful Trio libraries for web programming with Python:
hypercorn: Hypercorn is an ASGI server that supports HTTP
muffin: Muffin is an ASGI web framework for Python
startlette: Starlette is a lightweight ASGI framework
trio-websocket: support for <a href="https://geekflare.com/websocket-servers/">WebSocket server</a> and client</li><li><code>httpx: HTTP client for Python
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:
$ pip install Twisted
The following are Twisted’s salient features:
Developer-friendly features such as unit testing and static code checking
twisted.web provides a WSGI server and support for HTTP clients and servers
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 functionality
Support for logging to facilitate easier debugging; provides logging for a client, server, web sockets, and more
Native support for testing through the pytest Plugin for testing: pytest-aiohttp, Which is available as a PyPI package
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:
$ pip install tornado
The following are Tornado’s salient features:
Support for HTTP server tornado.httpserver and client
Scalable and uses non-blocking network I/O
Well suited for applications that require connectivity for a substantially longer interval of time
Seamless integration with services for authentication
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:
$ pip install meinheld
Some features of Meinheld:
Uses http-parser library to handle incoming HTTP requests
Builds on top of the picoev library for event handling
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!
Bala Priya C
Bala Priya is a developer and technical writer from India with over three years of experience in the technical content writing space. She shares her learning with the developer community by authoring tech tutorials, how-to guides, and more…. read more
Python is a very versatile language, and Python developers often have to work with a variety of files and get information stored in them for processing. One popular file format you’re bound to encounter as a Python developer is the Portable Document Format popularly known as PDF