I have read that Python can be used for web applications. Yet I wonder how form variables are passed to Python scripts and how to, so to speak, "connect" Python to an Apache server so that it serves dynamic web pages, the same way PHP so easily does. How is this done and is it better than using PHP?
-
Hi Robert, I think your question of how do python web frameworks is interesting but the if its better than PHP is totally up to personal preference. Consider editing out that prevention to prevent a lock. Keep in mind that its very likely that the methodology to pass values from web pages to apache and then to a logic tier is probably very similar for python, ruby, php or any other language that has a web framework. – sunnyrjuneja Nov 08 '12 at 06:12
3 Answers
The answer you're probably looking for is mod_wsgi
with Apache or uwsgi
with nginx.
Using these you can run any application you like.
For example should apache be configured for it myapp.wsgi
looks as follows:
(per here: http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide)
def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
This is just about the minimum requirement to return a web page.

- 4,044
- 1
- 37
- 53
-
1`environ` has a `'REQUEST_METHOD'` check out this question: http://stackoverflow.com/questions/394465/python-post-data-using-mod-wsgi – Williams Nov 08 '12 at 07:26
-
3But you are better off using a web framework such as Flask or Django which handles a lot of stuff for you such as form parsing. – Graham Dumpleton Nov 08 '12 at 07:28
-
Web development in python is philosophically different to php -- it works completely differently. You're doing yourself an extraordinary disservice trying to use python the php-way. It's good to understand things at the HTTP level, but +1 Graham's comment above. – Williams Nov 08 '12 at 07:35
CGI
The simplest possible way to connect a Python application to a web server is to use the CGI protocol. When a request comes in to the server, it will launch your application, feeding it the request body on STDIN, and accepting the response on STDOUT. This technique has been used since the earliest days of the web, and still works today. Its biggest downside is that your entire application is launched and shut down with every single request, so it is not suitable for large amounts of traffic.
FastCGI
An attempt to rectify the biggest problem with CGI is FastCGI -- it uses a similar protocol, but your application stays resident, and communicates with the web server over a socket (Unix named sockets and TCP sockets are both supported, I believe). This technique is supported by a number of web servers, but is generally considered inferior to WSGI these days. Apache supports it with the mod_fcgi module.
mod_python
Python web applications and frameworks traditionally connected with Apache using mod_python. Like mod_perl, this module provides Python bindings to the Apache API.
With it, you can write handlers for any phase of the request/response cycle. You can do URL rewriting, authentication and authorization, header manipulation, and actual response-building, using these Python handlers.
This is no longer recommended, though, as the mod_python package is not being maintained, while the Apache web server continues to evolve.
mod_wsgi
WSGI is a protocol for communicating between web servers and Python handlers. Documented in PEP-333, it is not tied to any particular web server, like mod_python is. Apache uses mod_wsgi; Nginx has it's own WSGI Module, and other servers have similar packages available.
Like FastCGI, wsgi processes remain resident between requests. It does not make explicit use of sockets, though -- instead, the wsgi package provides your application with an 'environment' dictionary, which includes a number of details about the request, and a pair of file-like objects: one for reading the request body, and one to write the response.
Also unlike FastCGI, in which a web server talks to a single application, WSGI supports applications configured as 'middleware'. These apps can receive requests, process them, and then pass them on to another WSGI process, allowing you to build an entire web application out of small processes, each of which is responsible for a piece of the processing.
WSGI is currently the preferred way to run python web applications.

- 43,011
- 8
- 86
- 87
Use a web framework like pyramid
or web2py
or django
.
These web frameworks comes bundled with (and optional) template engines and form frameworks and view functions (known as controllers in other frameworks like php codeignitor.
Serving Forms for a User to fill up
form classes are written in and form objects can be instantiated from these form classes and passed to the template in context.
Receiving Form Submissions
Like php or any other language which generates a html template, the instantiated form is displayed of course as a html form. When the user clicks on the "submit" button, the values in the form input fields are sent to the url the form is pointing at.
The url is mapped by your chosen python framework accordingly and receives the http request's POST (form submission) objects as string. You can then decide what you want to do with these incoming request POST objects.
Same Principles
No matter which framework you use and which language you choose, the basic principle is the same. Render HTML to display a form, receive the form's POST values and decide what you want to do with the values accordingly.

- 8,620
- 9
- 54
- 69

- 35,640
- 39
- 116
- 167
-
1I am not really looking for a framework. I want to know how you can use python as a web application development tool with you doing all the coding from scratch just as easily as we are able to do it with PHP. – Robert Nov 08 '12 at 06:17
-
@robertrocha: the simplest way to do it is to use http://webpy.org/ which I know you will complain is "still a web framework" but as you can see on web.py's home page, the simplest example is illustrated as "A complete web.py application". Only 10 lines of code to serve up your first web page. – Calvin Cheng Nov 08 '12 at 06:25