blob: 7f68b3b1af3c43eb3405c904605ed47459c13f28 [file] [log] [blame]
Benjamin Petersonae5360b2008-09-08 23:05:23 +00001*******************************
2 HOWTO Use Python in the web
3*******************************
4
5:Author: Marek Kubica
6
7.. topic:: Abstract
8
Ezio Melotti84ec57c2010-04-05 04:06:12 +00009 This document shows how Python fits into the web. It presents some ways
10 to integrate Python with a web server, and general practices useful for
Benjamin Petersonae5360b2008-09-08 23:05:23 +000011 developing web sites.
12
13
Ezio Melotti84ec57c2010-04-05 04:06:12 +000014Programming for the Web has become a hot topic since the rise of "Web 2.0",
Benjamin Petersonae5360b2008-09-08 23:05:23 +000015which focuses on user-generated content on web sites. It has always been
16possible to use Python for creating web sites, but it was a rather tedious task.
Ezio Melotti84ec57c2010-04-05 04:06:12 +000017Therefore, many frameworks and helper tools have been created to assist
18developers in creating faster and more robust sites. This HOWTO describes
19some of the methods used to combine Python with a web server to create
20dynamic content. It is not meant as a complete introduction, as this topic is
21far too broad to be covered in one single document. However, a short overview
22of the most popular libraries is provided.
Benjamin Petersonae5360b2008-09-08 23:05:23 +000023
24.. seealso::
25
Ezio Melotti84ec57c2010-04-05 04:06:12 +000026 While this HOWTO tries to give an overview of Python in the web, it cannot
27 always be as up to date as desired. Web development in Python is rapidly
28 moving forward, so the wiki page on `Web Programming
29 <http://wiki.python.org/moin/WebProgramming>`_ may be more in sync with
Benjamin Petersonae5360b2008-09-08 23:05:23 +000030 recent development.
31
32
Ezio Melotti84ec57c2010-04-05 04:06:12 +000033The Low-Level View
Benjamin Petersonae5360b2008-09-08 23:05:23 +000034==================
35
Ezio Melotti84ec57c2010-04-05 04:06:12 +000036When a user enters a web site, their browser makes a connection to the site's
37web server (this is called the *request*). The server looks up the file in the
Benjamin Petersonae5360b2008-09-08 23:05:23 +000038file system and sends it back to the user's browser, which displays it (this is
Ezio Melottia89918a2010-03-23 23:31:02 +000039the *response*). This is roughly how the underlying protocol, HTTP, works.
Benjamin Petersonae5360b2008-09-08 23:05:23 +000040
Ezio Melotti84ec57c2010-04-05 04:06:12 +000041Dynamic web sites are not based on files in the file system, but rather on
42programs which are run by the web server when a request comes in, and which
43*generate* the content that is returned to the user. They can do all sorts of
44useful things, like display the postings of a bulletin board, show your email,
45configure software, or just display the current time. These programs can be
46written in any programming language the server supports. Since most servers
47support Python, it is easy to use Python to create dynamic web sites.
Benjamin Petersonae5360b2008-09-08 23:05:23 +000048
Ezio Melotti84ec57c2010-04-05 04:06:12 +000049Most HTTP servers are written in C or C++, so they cannot execute Python code
50directly -- a bridge is needed between the server and the program. These
51bridges, or rather interfaces, define how programs interact with the server.
52There have been numerous attempts to create the best possible interface, but
53there are only a few worth mentioning.
Benjamin Petersonae5360b2008-09-08 23:05:23 +000054
Ezio Melotti84ec57c2010-04-05 04:06:12 +000055Not every web server supports every interface. Many web servers only support
56old, now-obsolete interfaces; however, they can often be extended using
57third-party modules to support newer ones.
Benjamin Petersonae5360b2008-09-08 23:05:23 +000058
59
60Common Gateway Interface
61------------------------
62
Ezio Melotti84ec57c2010-04-05 04:06:12 +000063This interface, most commonly referred to as "CGI", is the oldest, and is
64supported by nearly every web server out of the box. Programs using CGI to
65communicate with their web server need to be started by the server for every
66request. So, every request starts a new Python interpreter -- which takes some
67time to start up -- thus making the whole interface only usable for low load
68situations.
Benjamin Petersonae5360b2008-09-08 23:05:23 +000069
Ezio Melotti84ec57c2010-04-05 04:06:12 +000070The upside of CGI is that it is simple -- writing a Python program which uses
71CGI is a matter of about three lines of code. This simplicity comes at a
72price: it does very few things to help the developer.
Benjamin Petersonae5360b2008-09-08 23:05:23 +000073
Ezio Melotti84ec57c2010-04-05 04:06:12 +000074Writing CGI programs, while still possible, is no longer recommended. With
75:ref:`WSGI <WSGI>`, a topic covered later in this document, it is possible to write
76programs that emulate CGI, so they can be run as CGI if no better option is
77available.
Benjamin Petersonae5360b2008-09-08 23:05:23 +000078
79.. seealso::
80
81 The Python standard library includes some modules that are helpful for
82 creating plain CGI programs:
83
84 * :mod:`cgi` -- Handling of user input in CGI scripts
Ezio Melotti84ec57c2010-04-05 04:06:12 +000085 * :mod:`cgitb` -- Displays nice tracebacks when errors happen in CGI
Benjamin Petersonae5360b2008-09-08 23:05:23 +000086 applications, instead of presenting a "500 Internal Server Error" message
87
88 The Python wiki features a page on `CGI scripts
89 <http://wiki.python.org/moin/CgiScripts>`_ with some additional information
90 about CGI in Python.
91
Georg Brandl48310cd2009-01-03 21:18:54 +000092
Benjamin Petersonae5360b2008-09-08 23:05:23 +000093Simple script for testing CGI
94^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
95
96To test whether your web server works with CGI, you can use this short and
97simple CGI program::
98
99 #!/usr/bin/env python
100 # -*- coding: UTF-8 -*-
101
102 # enable debugging
Benjamin Petersonad3d5c22009-02-26 03:38:59 +0000103 import cgitb
104 cgitb.enable()
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000105
Georg Brandl6d204bf2008-12-05 18:04:41 +0000106 print("Content-Type: text/plain;charset=utf-8")
107 print()
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000108
Georg Brandl6d204bf2008-12-05 18:04:41 +0000109 print("Hello World!")
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000110
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000111Depending on your web server configuration, you may need to save this code with
112a ``.py`` or ``.cgi`` extension. Additionally, this file may also need to be
113in a ``cgi-bin`` folder, for security reasons.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000114
115You might wonder what the ``cgitb`` line is about. This line makes it possible
116to display a nice traceback instead of just crashing and displaying an "Internal
117Server Error" in the user's browser. This is useful for debugging, but it might
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000118risk exposing some confidential data to the user. You should not use ``cgitb``
119in production code for this reason. You should *always* catch exceptions, and
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000120display proper error pages -- end-users don't like to see nondescript "Internal
121Server Errors" in their browsers.
122
123
124Setting up CGI on your own server
125^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
126
127If you don't have your own web server, this does not apply to you. You can
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000128check whether it works as-is, and if not you will need to talk to the
129administrator of your web server. If it is a big host, you can try filing a
130ticket asking for Python support.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000131
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000132If you are your own administrator or want to set up CGI for testing purposes on
133your own computers, you have to configure it by yourself. There is no single
134way to configure CGI, as there are many web servers with different
135configuration options. Currently the most widely used free web server is
136`Apache HTTPd <http://httpd.apache.org/>`_, or Apache for short. Apache can be
137easily installed on nearly every system using the system's package management
138tool. `lighttpd <http://www.lighttpd.net>`_ is another alternative and is
139said to have better performance. On many systems this server can also be
140installed using the package management tool, so manually compiling the web
141server may not be needed.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000142
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000143* On Apache you can take a look at the `Dynamic Content with CGI
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000144 <http://httpd.apache.org/docs/2.2/howto/cgi.html>`_ tutorial, where everything
145 is described. Most of the time it is enough just to set ``+ExecCGI``. The
146 tutorial also describes the most common gotchas that might arise.
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000147
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000148* On lighttpd you need to use the `CGI module
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000149 <http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModCGI>`_\ , which can be configured
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000150 in a straightforward way. It boils down to setting ``cgi.assign`` properly.
151
152
153Common problems with CGI scripts
154^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
155
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000156Using CGI sometimes leads to small annoyances while trying to get these
157scripts to run. Sometimes a seemingly correct script does not work as
158expected, the cause being some small hidden problem that's difficult to spot.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000159
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000160Some of these potential problems are:
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000161
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000162* The Python script is not marked as executable. When CGI scripts are not
163 executable most web servers will let the user download it, instead of
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000164 running it and sending the output to the user. For CGI scripts to run
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000165 properly on Unix-like operating systems, the ``+x`` bit needs to be set.
166 Using ``chmod a+x your_script.py`` may solve this problem.
167
168* On a Unix-like system, The line endings in the program file must be Unix
169 style line endings. This is important because the web server checks the
170 first line of the script (called shebang) and tries to run the program
171 specified there. It gets easily confused by Windows line endings (Carriage
172 Return & Line Feed, also called CRLF), so you have to convert the file to
173 Unix line endings (only Line Feed, LF). This can be done automatically by
174 uploading the file via FTP in text mode instead of binary mode, but the
175 preferred way is just telling your editor to save the files with Unix line
176 endings. Most editors support this.
177
178* Your web server must be able to read the file, and you need to make sure the
179 permissions are correct. On unix-like systems, the server often runs as user
180 and group ``www-data``, so it might be worth a try to change the file
181 ownership, or making the file world readable by using ``chmod a+r
182 your_script.py``.
183
184* The web server must know that the file you're trying to access is a CGI script.
185 Check the configuration of your web server, as it may be configured
186 to expect a specific file extension for CGI scripts.
187
188* On Unix-like systems, the path to the interpreter in the shebang
189 (``#!/usr/bin/env python``) must be correct. This line calls
190 ``/usr/bin/env`` to find Python, but it will fail if there is no
191 ``/usr/bin/env``, or if Python is not in the web server's path. If you know
192 where your Python is installed, you can also use that full path. The
193 commands ``whereis python`` and ``type -p python`` could help you find
194 where it is installed. Once you know the path, you can change the shebang
195 accordingly: ``#!/usr/bin/python``.
196
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000197* The file must not contain a BOM (Byte Order Mark). The BOM is meant for
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000198 determining the byte order of UTF-16 and UTF-32 encodings, but some editors
199 write this also into UTF-8 files. The BOM interferes with the shebang line,
200 so be sure to tell your editor not to write the BOM.
201
202* If the web server is using :ref:`mod-python`, ``mod_python`` may be having
203 problems. ``mod_python`` is able to handle CGI scripts by itself, but it can
204 also be a source of issues.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000205
206
207.. _mod-python:
208
209mod_python
210----------
211
212People coming from PHP often find it hard to grasp how to use Python in the web.
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000213Their first thought is mostly `mod_python <http://www.modpython.org/>`_\ ,
214because they think that this is the equivalent to ``mod_php``. Actually, there
215are many differences. What ``mod_python`` does is embed the interpreter into
216the Apache process, thus speeding up requests by not having to start a Python
217interpreter for each request. On the other hand, it is not "Python intermixed
218with HTML" in the way that PHP is often intermixed with HTML. The Python
219equivalent of that is a template engine. ``mod_python`` itself is much more
220powerful and provides more access to Apache internals. It can emulate CGI,
221work in a "Python Server Pages" mode (similar to JSP) which is "HTML
222intermingled with Python", and it has a "Publisher" which designates one file
223to accept all requests and decide what to do with them.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000224
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000225``mod_python`` does have some problems. Unlike the PHP interpreter, the Python
226interpreter uses caching when executing files, so changes to a file will
227require the web server to be restarted. Another problem is the basic concept
228-- Apache starts child processes to handle the requests, and unfortunately
229every child process needs to load the whole Python interpreter even if it does
230not use it. This makes the whole web server slower. Another problem is that,
231because ``mod_python`` is linked against a specific version of ``libpython``,
232it is not possible to switch from an older version to a newer (e.g. 2.4 to 2.5)
233without recompiling ``mod_python``. ``mod_python`` is also bound to the Apache
234web server, so programs written for ``mod_python`` cannot easily run on other
235web servers.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000236
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000237These are the reasons why ``mod_python`` should be avoided when writing new
238programs. In some circumstances it still might be a good idea to use
239``mod_python`` for deployment, but WSGI makes it possible to run WSGI programs
240under ``mod_python`` as well.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000241
242
243FastCGI and SCGI
244----------------
245
246FastCGI and SCGI try to solve the performance problem of CGI in another way.
247Instead of embedding the interpreter into the web server, they create
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000248long-running background processes. There is still a module in the web server
249which makes it possible for the web server to "speak" with the background
250process. As the background process is independent of the server, it can be
251written in any language, including Python. The language just needs to have a
252library which handles the communication with the webserver.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000253
254The difference between FastCGI and SCGI is very small, as SCGI is essentially
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000255just a "simpler FastCGI". As the web server support for SCGI is limited,
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000256most people use FastCGI instead, which works the same way. Almost everything
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000257that applies to SCGI also applies to FastCGI as well, so we'll only cover
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000258the latter.
259
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000260These days, FastCGI is never used directly. Just like ``mod_python``, it is only
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000261used for the deployment of WSGI applications.
262
263.. seealso::
264
265 * `FastCGI, SCGI, and Apache: Background and Future
266 <http://www.vmunix.com/mark/blog/archives/2006/01/02/fastcgi-scgi-and-apache-background-and-future/>`_
267 is a discussion on why the concept of FastCGI and SCGI is better that that
268 of mod_python.
269
270
271Setting up FastCGI
272^^^^^^^^^^^^^^^^^^
273
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000274Each web server requires a specific module.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000275
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000276* Apache has both `mod_fastcgi <http://www.fastcgi.com/drupal/>`_ and `mod_fcgid
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000277 <http://fastcgi.coremail.cn/>`_. ``mod_fastcgi`` is the original one, but it
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000278 has some licensing issues, which is why it is sometimes considered non-free.
279 ``mod_fcgid`` is a smaller, compatible alternative. One of these modules needs
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000280 to be loaded by Apache.
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000281
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000282* lighttpd ships its own `FastCGI module
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000283 <http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModFastCGI>`_ as well as an
284 `SCGI module <http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModSCGI>`_.
285
286* `nginx <http://nginx.org/>`_ also supports `FastCGI
287 <http://wiki.nginx.org/NginxSimplePythonFCGI>`_.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000288
289Once you have installed and configured the module, you can test it with the
290following WSGI-application::
291
292 #!/usr/bin/env python
293 # -*- coding: UTF-8 -*-
294
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000295 import sys, os
Georg Brandl6d204bf2008-12-05 18:04:41 +0000296 from cgi import escape
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000297 from flup.server.fcgi import WSGIServer
298
299 def app(environ, start_response):
300 start_response('200 OK', [('Content-Type', 'text/html')])
301
302 yield '<h1>FastCGI Environment</h1>'
303 yield '<table>'
304 for k, v in sorted(environ.items()):
Georg Brandl6d204bf2008-12-05 18:04:41 +0000305 yield '<tr><th>{0}</th><td>{1}</td></tr>'.format(
306 escape(k), escape(v))
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000307 yield '</table>'
308
309 WSGIServer(app).run()
310
311This is a simple WSGI application, but you need to install `flup
312<http://pypi.python.org/pypi/flup/1.0>`_ first, as flup handles the low level
313FastCGI access.
314
315.. seealso::
316
317 There is some documentation on `setting up Django with FastCGI
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000318 <http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/>`_, most of
319 which can be reused for other WSGI-compliant frameworks and libraries.
320 Only the ``manage.py`` part has to be changed, the example used here can be
321 used instead. Django does more or less the exact same thing.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000322
323
324mod_wsgi
325--------
326
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000327`mod_wsgi <http://code.google.com/p/modwsgi/>`_ is an attempt to get rid of the
328low level gateways. Given that FastCGI, SCGI, and mod_python are mostly used to
329deploy WSGI applications, mod_wsgi was started to directly embed WSGI applications
330into the Apache web server. mod_wsgi is specifically designed to host WSGI
331applications. It makes the deployment of WSGI applications much easier than
332deployment using other low level methods, which need glue code. The downside
333is that mod_wsgi is limited to the Apache web server; other servers would need
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000334their own implementations of mod_wsgi.
335
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000336mod_wsgi supports two modes: embedded mode, in which it integrates with the
337Apache process, and daemon mode, which is more FastCGI-like. Unlike FastCGI,
338mod_wsgi handles the worker-processes by itself, which makes administration
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000339easier.
340
341
342.. _WSGI:
343
344Step back: WSGI
345===============
346
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000347WSGI has already been mentioned several times, so it has to be something
348important. In fact it really is, and now it is time to explain it.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000349
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000350The *Web Server Gateway Interface*, or WSGI for short, is defined in
351:pep:`333` and is currently the best way to do Python web programming. While
352it is great for programmers writing frameworks, a normal web developer does not
353need to get in direct contact with it. When choosing a framework for web
354development it is a good idea to choose one which supports WSGI.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000355
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000356The big benefit of WSGI is the unification of the application programming
357interface. When your program is compatible with WSGI -- which at the outer
358level means that the framework you are using has support for WSGI -- your
359program can be deployed via any web server interface for which there are WSGI
360wrappers. You do not need to care about whether the application user uses
361mod_python or FastCGI or mod_wsgi -- with WSGI your application will work on
362any gateway interface. The Python standard library contains its own WSGI
363server, :mod:`wsgiref`, which is a small web server that can be used for
364testing.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000365
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000366A really great WSGI feature is middleware. Middleware is a layer around your
367program which can add various functionality to it. There is quite a bit of
368`middleware <http://wsgi.org/wsgi/Middleware_and_Utilities>`_ already
369available. For example, instead of writing your own session management (HTTP
370is a stateless protocol, so to associate multiple HTTP requests with a single
371user your application must create and manage such state via a session), you can
372just download middleware which does that, plug it in, and get on with coding
373the unique parts of your application. The same thing with compression -- there
374is existing middleware which handles compressing your HTML using gzip to save
375on your server's bandwidth. Authentication is another a problem easily solved
376using existing middleware.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000377
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000378Although WSGI may seem complex, the initial phase of learning can be very
379rewarding because WSGI and the associated middleware already have solutions to
380many problems that might arise while developing web sites.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000381
382
383WSGI Servers
384------------
385
386The code that is used to connect to various low level gateways like CGI or
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000387mod_python is called a *WSGI server*. One of these servers is ``flup``, which
388supports FastCGI and SCGI, as well as `AJP
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000389<http://en.wikipedia.org/wiki/Apache_JServ_Protocol>`_. Some of these servers
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000390are written in Python, as ``flup`` is, but there also exist others which are
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000391written in C and can be used as drop-in replacements.
392
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000393There are many servers already available, so a Python web application
394can be deployed nearly anywhere. This is one big advantage that Python has
395compared with other web technologies.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000396
397.. seealso::
398
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000399 A good overview of WSGI-related code can be found in the `WSGI wiki
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000400 <http://wsgi.org/wsgi>`_, which contains an extensive list of `WSGI servers
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000401 <http://wsgi.org/wsgi/Servers>`_ which can be used by *any* application
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000402 supporting WSGI.
403
404 You might be interested in some WSGI-supporting modules already contained in
405 the standard library, namely:
Georg Brandl48310cd2009-01-03 21:18:54 +0000406
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000407 * :mod:`wsgiref` -- some tiny utilities and servers for WSGI
408
409
410Case study: MoinMoin
411--------------------
412
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000413What does WSGI give the web application developer? Let's take a look at
414an application that's been around for a while, which was written in
415Python without using WSGI.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000416
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000417One of the most widely used wiki software packages is `MoinMoin
418<http://moinmo.in/>`_. It was created in 2000, so it predates WSGI by about
419three years. Older versions needed separate code to run on CGI, mod_python,
420FastCGI and standalone.
421
422It now includes support for WSGI. Using WSGI, it is possible to deploy
423MoinMoin on any WSGI compliant server, with no additional glue code.
424Unlike the pre-WSGI versions, this could include WSGI servers that the
425authors of MoinMoin know nothing about.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000426
427
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000428Model-View-Controller
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000429=====================
430
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000431The term *MVC* is often encountered in statements such as "framework *foo*
432supports MVC". MVC is more about the overall organization of code, rather than
433any particular API. Many web frameworks use this model to help the developer
434bring structure to their program. Bigger web applications can have lots of
435code, so it is a good idea to have an effective structure right from the beginning.
436That way, even users of other frameworks (or even other languages, since MVC is
437not Python-specific) can easily understand the code, given that they are
438already familiar with the MVC structure.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000439
440MVC stands for three components:
441
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000442* The *model*. This is the data that will be displayed and modified. In
443 Python frameworks, this component is often represented by the classes used by
444 an object-relational mapper.
445
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000446* The *view*. This component's job is to display the data of the model to the
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000447 user. Typically this component is implemented via templates.
448
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000449* The *controller*. This is the layer between the user and the model. The
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000450 controller reacts to user actions (like opening some specific URL), tells
451 the model to modify the data if necessary, and tells the view code what to
452 display,
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000453
454While one might think that MVC is a complex design pattern, in fact it is not.
455It is used in Python because it has turned out to be useful for creating clean,
456maintainable web sites.
457
458.. note::
459
460 While not all Python frameworks explicitly support MVC, it is often trivial
Mark Dickinson934896d2009-02-21 20:59:32 +0000461 to create a web site which uses the MVC pattern by separating the data logic
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000462 (the model) from the user interaction logic (the controller) and the
Mark Dickinson934896d2009-02-21 20:59:32 +0000463 templates (the view). That's why it is important not to write unnecessary
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000464 Python code in the templates -- it works against the MVC model and creates
465 chaos in the code base, making it harder to understand and modify.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000466
467.. seealso::
468
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000469 The English Wikipedia has an article about the `Model-View-Controller pattern
470 <http://en.wikipedia.org/wiki/Model-view-controller>`_. It includes a long
471 list of web frameworks for various programming languages.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000472
473
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000474Ingredients for Websites
475========================
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000476
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000477Websites are complex constructs, so tools have been created to help web
478developers make their code easier to write and more maintainable. Tools like
479these exist for all web frameworks in all languages. Developers are not forced
480to use these tools, and often there is no "best" tool. It is worth learning
481about the available tools because they can greatly simplify the process of
482developing a web site.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000483
484
485.. seealso::
486
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000487 There are far more components than can be presented here. The Python wiki
488 has a page about these components, called
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000489 `Web Components <http://wiki.python.org/moin/WebComponents>`_.
490
491
492Templates
493---------
494
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000495Mixing of HTML and Python code is made possible by a few libraries. While
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000496convenient at first, it leads to horribly unmaintainable code. That's why
497templates exist. Templates are, in the simplest case, just HTML files with
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000498placeholders. The HTML is sent to the user's browser after filling in the
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000499placeholders.
500
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000501Python already includes a way to build simple templates::
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000502
503 # a simple template
Georg Brandl6d204bf2008-12-05 18:04:41 +0000504 template = "<html><body><h1>Hello {who}!</h1></body></html>"
505 print(template.format(who="Reader"))
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000506
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000507To generate complex HTML based on non-trivial model data, conditional
508and looping constructs like Python's *for* and *if* are generally needed.
509*Template engines* support templates of this complexity.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000510
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000511There are a lot of template engines available for Python which can be used with
512or without a `framework`_. Some of these define a plain-text programming
513language which is easy to learn, partly because it is limited in scope.
514Others use XML, and the template output is guaranteed to be always be valid
515XML. There are many other variations.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000516
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000517Some `frameworks`_ ship their own template engine or recommend one in
518particular. In the absence of a reason to use a different template engine,
519using the one provided by or recommended by the framework is a good idea.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000520
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000521Popular template engines include:
Georg Brandl48310cd2009-01-03 21:18:54 +0000522
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000523 * `Mako <http://www.makotemplates.org/>`_
524 * `Genshi <http://genshi.edgewall.org/>`_
525 * `Jinja <http://jinja.pocoo.org/2/>`_
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000526
527.. seealso::
528
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000529 There are many template engines competing for attention, becuase it is
530 pretty easy to create them in Python. The page `Templating
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000531 <http://wiki.python.org/moin/Templating>`_ in the wiki lists a big,
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000532 ever-growing number of these. The three listed above are considered "second
533 generation" template engines and are a good place to start.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000534
535
536Data persistence
537----------------
538
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000539*Data persistence*, while sounding very complicated, is just about storing data.
540This data might be the text of blog entries, the postings on a bulletin board or
541the text of a wiki page. There are, of course, a number of different ways to store
542information on a web server.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000543
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000544Often, relational database engines like `MySQL <http://www.mysql.com/>`_ or
545`PostgreSQL <http://www.postgresql.org/>`_ are used because of their good
546performance when handling very large databases consisting of millions of
547entries. There is also a small database engine called `SQLite
548<http://www.sqlite.org/>`_, which is bundled with Python in the :mod:`sqlite3`
549module, and which uses only one file. It has no other dependencies. For
550smaller sites SQLite is just enough.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000551
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000552Relational databases are *queried* using a language called `SQL
553<http://en.wikipedia.org/wiki/SQL>`_. Python programmers in general do not
554like SQL too much, as they prefer to work with objects. It is possible to save
555Python objects into a database using a technology called `ORM
556<http://en.wikipedia.org/wiki/Object-relational_mapping>`_ (Object Relational
557Mapping). ORM translates all object-oriented access into SQL code under the
558hood, so the developer does not need to think about it. Most `frameworks`_ use
559ORMs, and it works quite well.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000560
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000561A second possibility is storing data in normal, plain text files (some
562times called "flat files"). This is very easy for simple sites,
563but can be difficult to get right if the web site is performing many
564updates to the stored data.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000565
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000566A third possibility are object oriented databases (also called "object
567databases"). These databases store the object data in a form that closely
568parallels the way the objects are structured in memory during program
569execution. (By contrast, ORMs store the object data as rows of data in tables
570and relations between those rows.) Storing the objects directly has the
571advantage that nearly all objects can be saved in a straightforward way, unlike
572in relational databases where some objects are very hard to represent.
573
574`Frameworks`_ often give hints on which data storage method to choose. It is
575usually a good idea to stick to the data store recommended by the framework
576unless the application has special requirements better satisfied by an
577alternate storage mechanism.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000578
579.. seealso::
580
581 * `Persistence Tools <http://wiki.python.org/moin/PersistenceTools>`_ lists
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000582 possibilities on how to save data in the file system. Some of these
583 modules are part of the standard library
584
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000585 * `Database Programming <http://wiki.python.org/moin/DatabaseProgramming>`_
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000586 helps with choosing a method for saving data
587
588 * `SQLAlchemy <http://www.sqlalchemy.org/>`_, the most powerful OR-Mapper
589 for Python, and `Elixir <http://elixir.ematia.de/>`_, which makes
590 SQLAlchemy easier to use
591
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000592 * `SQLObject <http://www.sqlobject.org/>`_, another popular OR-Mapper
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000593
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000594 * `ZODB <https://launchpad.net/zodb>`_ and `Durus
595 <http://www.mems-exchange.org/software/durus/>`_, two object oriented
596 databases
597
598
599.. _framework:
600
601Frameworks
602==========
603
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000604The process of creating code to run web sites involves writing code to provide
605various services. The code to provide a particular service often works the
606same way regardless of the complexity or purpose of the web site in question.
607Abstracting these common solutions into reusable code produces what are called
608"frameworks" for web development. Perhaps the most well-known framework for
609web development is Ruby on Rails, but Python has its own frameworks. Some of
610these were partly inspired by Rails, or borrowed ideas from Rails, but many
611existed a long time before Rails.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000612
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000613Originally Python web frameworks tended to incorporate all of the services
614needed to develop web sites as a giant, integrated set of tools. No two web
615frameworks were interoperable: a program developed for one could not be
616deployed on a different one without considerable re-engineering work. This led
617to the development of "minimalist" web frameworks that provided just the tools
618to communicate between the Python code and the http protocol, with all other
619services to be added on top via separate components. Some ad hoc standards
620were developed that allowed for limited interoperability between frameworks,
621such as a standard that allowed different template engines to be used
622interchangeably.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000623
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000624Since the advent of WSGI, the Python web framework world has been evolving
625toward interoperability based on the WSGI standard. Now many web frameworks,
626whether "full stack" (providing all the tools one needs to deploy the most
627complex web sites) or minimalist, or anything in between, are built from
628collections of reusable components that can be used with more than one
629framework.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000630
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000631The majority of users will probably want to select a "full stack" framework
632that has an active community. These frameworks tend to be well documented,
633and provide the easiest path to producing a fully functional web site in
634minimal time.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000635
636
637Some notable frameworks
638-----------------------
639
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000640There are an incredible number of frameworks, so they cannot all be covered
641here. Instead we will briefly touch on some of the most popular.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000642
643
644Django
645^^^^^^
646
647`Django <http://www.djangoproject.com/>`_ is a framework consisting of several
648tightly coupled elements which were written from scratch and work together very
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000649well. It includes an ORM which is quite powerful while being simple to use,
650and has a great online administration interface which makes it possible to edit
651the data in the database with a browser. The template engine is text-based and
652is designed to be usable for page designers who cannot write Python. It
653supports template inheritance and filters (which work like Unix pipes). Django
654has many handy features bundled, such as creation of RSS feeds or generic views,
655which make it possible to create web sites almost without writing any Python code.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000656
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000657It has a big, international community, the members of which have created many
658web sites. There are also a lot of add-on projects which extend Django's normal
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000659functionality. This is partly due to Django's well written `online
Georg Brandl628e6f92009-10-27 20:24:45 +0000660documentation <http://docs.djangoproject.com/>`_ and the `Django book
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000661<http://www.djangobook.com/>`_.
662
663
664.. note::
665
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000666 Although Django is an MVC-style framework, it names the elements
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000667 differently, which is described in the `Django FAQ
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000668 <http://docs.djangoproject.com/en/dev/faq/general/#django-appears-to-be-a-mvc-framework-but-you-call-the-controller-the-view-and-the-view-the-template-how-come-you-don-t-use-the-standard-names>`_.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000669
670
671TurboGears
672^^^^^^^^^^
673
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000674Another popular web framework for Python is `TurboGears
675<http://www.turbogears.org/>`_. TurboGears takes the approach of using already
676existing components and combining them with glue code to create a seamless
677experience. TurboGears gives the user flexibility in choosing components. For
678example the ORM and template engine can be changed to use packages different
679from those used by default.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000680
681The documentation can be found in the `TurboGears wiki
682<http://docs.turbogears.org/>`_, where links to screencasts can be found.
683TurboGears has also an active user community which can respond to most related
684questions. There is also a `TurboGears book <http://turbogearsbook.com/>`_
685published, which is a good starting point.
686
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000687The newest version of TurboGears, version 2.0, moves even further in direction
688of WSGI support and a component-based architecture. TurboGears 2 is based on
689the WSGI stack of another popular component-based web framework, `Pylons
690<http://pylonshq.com/>`_.
691
692
693Zope
694^^^^
695
696The Zope framework is one of the "old original" frameworks. Its current
697incarnation in Zope2 is a tightly integrated full-stack framework. One of its
698most interesting feature is its tight integration with a powerful object
699database called the `ZODB <https://launchpad.net/zodb>`_ (Zope Object Database).
700Because of its highly integrated nature, Zope wound up in a somewhat isolated
701ecosystem: code written for Zope wasn't very usable outside of Zope, and
702vice-versa. To solve this problem the Zope 3 effort was started. Zope 3
703re-engineers Zope as a set of more cleanly isolated components. This effort
704was started before the advent of the WSGI standard, but there is WSGI support
705for Zope 3 from the `Repoze <http://repoze.org/>`_ project. Zope components
706have many years of production use behind them, and the Zope 3 project gives
707access to these components to the wider Python community. There is even a
708separate framework based on the Zope components: `Grok
709<http://grok.zope.org/>`_.
710
711Zope is also the infrastructure used by the `Plone <http://plone.org/>`_ content
712management system, one of the most powerful and popular content management
713systems available.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000714
715
716Other notable frameworks
717^^^^^^^^^^^^^^^^^^^^^^^^
718
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000719Of course these are not the only frameworks that are available. There are
720many other frameworks worth mentioning.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000721
722Another framework that's already been mentioned is `Pylons`_. Pylons is much
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000723like TurboGears, but with an even stronger emphasis on flexibility, which comes
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000724at the cost of being more difficult to use. Nearly every component can be
Mark Dickinson934896d2009-02-21 20:59:32 +0000725exchanged, which makes it necessary to use the documentation of every single
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000726component, of which there are many. Pylons builds upon `Paste
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000727<http://pythonpaste.org/>`_, an extensive set of tools which are handy for WSGI.
728
729And that's still not everything. The most up-to-date information can always be
730found in the Python wiki.
731
732.. seealso::
Georg Brandl48310cd2009-01-03 21:18:54 +0000733
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000734 The Python wiki contains an extensive list of `web frameworks
735 <http://wiki.python.org/moin/WebFrameworks>`_.
736
737 Most frameworks also have their own mailing lists and IRC channels, look out
Ezio Melotti84ec57c2010-04-05 04:06:12 +0000738 for these on the projects' web sites. There is also a general "Python in the
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000739 Web" IRC channel on freenode called `#python.web
740 <http://wiki.python.org/moin/PoundPythonWeb>`_.