In this article, we will talk about three main things: Python, Web Servers, and most importantly the bits and bobs in-between the two.
Jokes aside, this rather long article might seem dire for some looking for quick guidance or answers. Unfortunately, unlike most things in the world of Python, when it comes to choosing a production server stack to deploy your application on, there is not one-- and preferably only one --obvious way to do it.
However, this should not scare you. After completing this article, you will have a good knowledge of how different web servers work and handle the task of talking with Python based web applications. Upon gauging your needs and requirements, you will be able to decide which server to use.
Today, there exists web servers (or modules for servers) in ever growing numbers specifically designed (or adapted) to work with Python web applications interchangeably. However, this has not always been the case. In ye olde days, developers did not really have the possibility to switch web servers at will easily, and each switch came with a cost due to dependencies and limitations. Upon deciding on a framework to build on, you would have also decided, not always willingly nor consciously, on the server(s) you could use to serve the application as well. This was due to the lack of existence of a universally accepted interface specification: a common ground which applications (frameworks) and web servers alike would adapt and use to communicate, allowing interchangeability of components when necessary with possibly zero code change.
At beginning of this century, efforts were made to finally solve the issue with the presentation of Python Enhancement Proposal (PEP) 333 to the community.
From the PEP (Python Enhancement Proposal) 333:
This document specifies a proposed standard
interface between web servers and Python web
applications or frameworks, to promote web
application portability across a variety of
web servers.
As explained in the PEP, this new standard was (and is) meant to allow portability across (and between) [web] servers and [Python web] applications. The standard's powerful features and its wide adoption compared to those from before lead the way for today: a world where many (perhaps too many) web servers willing to do the job for you exist.
In this comparison of web servers for Python based web applications, we will talk about some of the choices available and what makes them stand out. The aim here is for the reader to have a clearer vision and to provide help to match the servers against applications' custom needs to find the one. Due to the vast number of options (more popping up every day!), we will filter our way through and talk about those which are "special" in various ways: popularity, solidness, or doing something different (or better) compared to the rest.
Note: I would like to advise you, the reader, to be wary of biased and deceiving benchmarks which tend to not reflect the conditions of a real production environment. Unfortunately, those [articles] do not really mean much when it comes to choosing a web server for production, which is also highly unlikely to be the part causing the bottleneck. You are, therefore, advised to gauge and understand your own needs and then to try different options, refraining from speculative numbers to avoid real future disaster scenarios.
What is it?
CherryPy is actually a web framework. Yet it is a fully self-contained one -- meaning that it can run on its own, including in production scenarios without the need of additional software. This is achieved thanks to its own WSGI, HTTP/1.1-compliant web server. CherryPy project describes it as a "A high-speed, production ready, thread pooled, generic HTTP server". As it is a WSGI server, it can be used to serve any other WSGI Python application as well, without being bound to CherryPy's application development framework.
Why should you consider using it?
What is it?
Gunicorn is a stand-alone web server which offers quite a bit of functionality in a significantly easy to operate fashion. It uses the pre-fork model -- meaning that a central master process (Gunicorn) is tasked with managing the initiated worker processes (of differing types), which then handle and deal with the requests directly. And all this can be configured and adapted to suit your needs and diverse production scenarios.
Why should you consider using it?
What is it?
Tornado is an application development framework and a networking library designed for handling asynchrnous operations, allowing servers to maintain a lot of open connections. It also comes with a WSGI server which other WSGI Python applications (and frameworks) can use to run.
Why should you consider using it?
Although under these circumstances you might want to choose Tornado's WSGI server for your project, you can also opt to use Gunicorn with Tornado [Asynchronous] workers.
What is it?
Twisted Web is the web server that comes with the Twisted networking library. Whereas Twisted itself is "an event-driven networking engine", theTwisted Web server runs on WSGI and it is capable of powering other Python web applications.
Why should you consider using it?
Python Scrips (.rpy)
which are executed upon HTTP requests.What is it?
Despite its very confusing naming conventions, uWSGI itself is a vast project with many components, aiming to provide a full [software] stack
forbuilding hosting services
. One of these components, the uWSGI server, runs Python WSGI applications. It is capable of using various protocols, including its own uwsgi wire protocol, which is quasi-identical to SCGI. In order to fulfil the (understandable) demand to use stand-alone HTTP servers in front of application servers, NGINX and Cherokee web servers are modularised to support uWSGI's (best performing) uwsgi protocol to have direct control over its processes.
Why should you consider using it?
What is it?
Waitress is a pure-Python WSGI server. At a first glance it might not appear to be that much different than many others; however, its development philosophy separates it from the rest. Its aim for easing the production (and development) burden caused by web servers for Python web-application developers. Waitress achieves this by neutralizing issues caused by platform (ex. Unix vs. Windows), interpreter (CPython vs. PyPy) and Python (version 2 vs. 3) differences.
Why should you consider using it?
mod_python with a WSGI adapter (Apache) (Embedding Python)
What is it?
Simply put, mod_python is an Apache module that embeds Python within the server itself. Although not recommended for various reasons (project is dead and outdated with only very recent intentions to continue the development by original author), it can be used to run WSGI applications on Apache via wrappers.
Why should you consider using it?
You might want to program and extend Apache using Python for a specific reason.
mod_wsgi (Apache) (Embedding Python)
What is it?
Being a WSGI compliant module, mod_wsgi allows you to run Python WSGI applications on Apache HTTP Server. It achieves this in two ways: the first being similar to how mod_python works, by embedding the code and executing it within the child process. The other method offers a daemon based operation mode whereby the WSGI application has its own distinct process, managed automatically by mod_wsgi.
Why should you consider using it?
Article ID: 261
Created On: Sat, Jan 4, 2014 at 1:22 AM
Last Updated On: Sat, Jan 4, 2014 at 1:22 AM
Authored by: ASPHostServer Administrator [asphostserver@gmail.com]
Online URL: http://faq.asphosthelpdesk.com/article.php?id=261