Programming for the Web has become a hot topic since the rise of “Web 2.0”, which focuses on user-generated content on web sites. It has always been possible to use Python for creating web sites, but it was a rather tedious task. Therefore, many frameworks and helper tools have been created to assist developers in creating faster and more robust sites. This HOWTO describes some of the methods used to combine Python with a web server to create dynamic content. It is not meant as a complete introduction, as this topic is far too broad to be covered in one single document.
A web application consists of code that responds to HTTP requests to return an HTTP response. A web application is not the same as a web server; the server is listening on the network socket for requests and decoding them and then sending back the response. The web application is the code that takes the request information and generates the response. To write a web application we need a way to send the request information from the server to the application code; there are many ways to do this for different combinations of web server and application environment.
In Python, the low-level interface to web applications is called WSGI (Web Server Gateway Interface). It defines a standard way of defining a procedure to act as as web application; the procedure takes two arguments and returns the content to be sent back as the response to the request. It is possible to use WSGI directly to write web applications. Here is a simple example:
def application(environ, start_response): headers = [('content-type', 'text/plain')] start_response('200 OK', headers) return [b"Hello World", b" how are you"]
The two arguments to this procedure are
environ, which contains information about the request environment (not used in this example), and
start_response which is a procedure that you must call to tell the server about your response to the request. The return value is a list of strings that will be sent back as the response – in this case we’re just sending back a plain text response.
While it is possible to write an application using the low-level WSGI standard, it is usually much more convenient to use a higher level abstraction in the form of a framework. This means that we use a python module that does some of the work for us and takes away some of the complexity of dealing with the low level WSGI standard. There are many frameworks to choose from for Python (see this list) but we will use the Bottle framework because it is simple and doesn’t do everything for us – the goal of this course is for you to learn how the web works, so we don’t yet want to use a framework that hides all of the detail from you.
Inclined to build a profession as Python Developer? Then here is the blog post on Python Training
We’ll explore Bottle in lots of detail but to show you a simple example, a simple Bottle web application would be:
from bottle import Bottle app = Bottle() @app.route('/') def index(): return "Hello World, how are you?" @app.route('/about') def index(): return "Tell me about yourself."
The first thing that this example does is create a Bottle application object. This is going to be used when we come to run the code. (Note that you will see examples of Bottle apps without this, we’ll use it because it helps with testing your code later on).
This example defines two ‘pages’ in the application – that is it will respond to two different URLs. Each URL is called a route and the code associates this route with a procedure that returns the content to be sent back to the browser. So if this application gets a request for the home page (eg.
http://localhost:8000/) then it will return the “Hello World…” message. If it gets a request for the ‘about page’ (eg.
http://localhost:8000/about) it will return the “Tell me…” message. In this simple example these are plain text; we’ll see how to make them return HTML pages shortly.
This structure for the Bottle application emphasises one important feature of web applications. The job of the application is to respond to requests forwarded by the server; each request is for a particular URL. The application must receive the request, process it, and generate a response. Bottle allows you to arrange your code so that one procedure deals with one request; this makes it easy to see what your code is doing and helps with debugging.
Bottle is built on top of the WSGI standard and so it is compatible with all kinds of other Python web infrastructure. For example there are modules that implement authentication and sessions and they can be used directly with Bottle applications. You don’t need to worry about this just now but it is reassuring to know that what you are doing is aligned with core Python infrastructure.
This is a simple example of an application, we now need to look at how to run it.
For an Indepth knowledge on Python, click on below