Learning Python Network Programming

(Sean Pound) #1

Applications for the Web


This external code is what we commonly refer to when we talk about web
applications. So the response process duties can be split, as shown in the
following figure:


Web applications can be written in any language that the web server is able to
invoke, providing great flexibility and allowing higher level languages to be used.
This can drastically reduce the time it takes to develop a new web service. These
days there is a great range of languages that can be used to write web applications,
and Python is no exception.


Python and the Web


Using some of the techniques discussed in this book, in particular Chapter 8, Client
and Server Applications, it is possible to use Python to write a full web server that
handles all four of the steps of handling an HTTP request that we listed in the
previous section. There are several actively developed web servers already in
existence written in pure Python, including Gunicorn (http://gunicorn.org),
and CherryPy (http://www.cherrypy.org). There is even a very basic HTTP
server in the standard library http.server module.


Writing a full HTTP server is not a trivial task and a detailed treatment is well
beyond the scope of this book. It is also not a very common requirement nowadays,
primarily due to the prevalence of excellent web servers that are already ready to
deploy. If you do feel the need to have a crack at this challenge though, I would start
with looking through the source code of the web servers mentioned earlier, looking
in more detail at the frameworks listed in Chapter 8, Client and Server Applications, and
reading the full HTTP specifications in the relevant RFCs. You may also want to read
the WSGI specifications, discussed in the WSGI section later on, so as to allow the
server to act as a host for other Python web applications.

Free download pdf