Now that you know how to write a simple application for the Web, you maY,want to write an application server in Python. Chances are you would use one like Django, Turbo-Gears, Pylons, etc. But you can use the Web server gateway interface as well, which is supported by various apI?lication servers. You can find out more about it at www.wsgi.org or just by searching for 'wsgi tutorials' Try the following simple code in a file teseapp.py:
To create a Web server, you will need to have a method called application (not mandatory, but preferable). The method requires two parameters, environ preferable). The method requires two parameters, environ and stare response, and returns a list of string to be displayed. The first parameter is a dictionary containing various environment variables. Your program displays a few of them related to the Web pag The second parameter is a method you call to, well, start the response for the request.
You can convert your code into a Web server using the simple reference server included in Python. Your server would normally keep handling requests and instead of calling handle_request, you would call serveJorever.
You may notice the use of () instead of [] to define env _ vars, using list comprehensions. The use of squar brackets creates a list while the use of parentheses creates a generator for the list. Using a generator save the creation of a temporary list, which can be especial beneficial when dealing with potentially large lists.
You would run the application from the command prompt, python teseapp.py, and by pointing the browser to localhost:8080Ianything?anyvar=anyvalue The result would be as follows:
You can change the CGI application you wrote to work with WSGI instead. Your file, application.py, should then include the following:
The core application code is not very different fro the earlier one. The form responds with a POST meth and calls form.py because the form had called it in th em version; and you too are using the same HTML page and you call processJorm. For all other URIs, you display a blank form. A URI does not correspond to a file or a method name any more. You can use path_info and the parameters returned to perform the actions required for the Web application.
The parameters returned by the GET method are in the QUERY_STRING environment variable. However, the POST method may return large amounts of data; so it is available in a file object referred by wsgi.input. The methodparse_qs parses a string and converts the parameters into a dictionary. Following is a minimal example of displayJorm andprocessJorm that you may write:
The data returned by POST is a list of values because a form may include multiple rows with the same field name. So, you pick the first value from the list.
The Web server frameworks make it much easier to handle URLs, manage complex Web pages and associate form fields with database columns. So, you would normally use one of the frameworks. Managing classifieds would be a pretty simple task for any of the frameworks mentioned.
It is hoped that by the wider adoption ofWSGI, it will be possible to write components that may be 'callable' from various frameworks or Web applications. Here's an example of plugging Trac within a Pylons framework: So, you can easily extend your classifieds application with a wiki, using moin-moin and a fault
tracking application using Trac!



Reply With Quote
Copyright Techfuels
Bookmarks