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.

The much stronger requirement is to build a web service application to generate some dynamic content, and to get it up and running quickly. In this situation, Python provides us with some excellent options in the form of web frameworks.

Web frameworks

A web framework is a layer that sits between the web server and our Python code, which provides abstractions and streamlined APIs to perform many of the common operations of interpreting HTTP requests and generating responses. Ideally, it is also structured so that it guides us into employing well-tested patterns for good web development. Frameworks for Python web applications are usually written in Python, and can be considered part of the web application.

The basic services a framework provides are:

  • Abstraction of HTTP requests and responses
  • Management of the URL space (routing)
  • Separation of Python code and markup (templating)

There are many Python web frameworks in use today, and here's a non-exhaustive list of some popular ones, in no particular order:

There are so many frameworks because there are many approaches that can be taken to the tasks they perform, and many different opinions about what tasks they should even perform.

Some frameworks provide the minimum to quickly build a simple web application. These are often called microframeworks, the most popular here being Armin Ronacher's excellent Flask. Although they may not include the functionality of some of the heavyweight frameworks, what they do, they generally do very well, and provide hooks to allow easy extension for more complex tasks. This allows a fully customizable approach to web application development.

Other frameworks take a much more batteries-included stance, providing for all the common needs of modern web applications. The major contender here is Django, which includes everything from templating to form management and database abstraction, and even a complete out-of-the-box web-based database admin interface. TurboGears provides similar functionality by integrating a core microframework with several established packages for the other features.

Yet other frameworks provide features such as supporting web applications with an event-driven architecture, such as Tornado, and CherryPy. Both of these also feature their own built-in production quality web servers.

Choosing a framework can be a tricky decision, and there is no right answer. We're going to take a quick look at one of today's most popular frameworks to get an idea of the services a framework can offer, then discuss how you might approach choosing one.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.79.11