apache 2 & python

md mark at deverter.net
Thu Aug 19 22:07:05 CEST 2004


Krzysztof Drozd wrote:
> how to configure apache 2 to work with python or mod_python?
> 
> 
> 
> krzysiek
> 
> ps: sory,my english is't perfect :)

I use Python for my Apache2 CGI scripts without mod_python (have not 
tried it yet).  Using the cgi module you can POST and GET quite easily 
and it makes for very simple to write dynamic web pages.

First, you will need to add an item to httpd.conf.  In httpd.conf add a 
ScriptAlias for your script name(you should already have one for 
/cgi-bin/ ):

...
ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin"
ScriptAlias /nameScript/ "/usr/local/apache2/cgi-bin/nameScript.py"
...

Then write your script in that location.  The script below would expect 
an http POST or url value like this:

http://www.whatever.com/?nameScript&user_name=Foo

The script below ties it all in....

+------

#!/usr/bin/python
import cgitb; cgitb.enable() #provides HTML error support
import cgi

form = cgi.FieldStorage()

def CGI(content_type="text/html"):
	return 'Content type: %s\n\n' % content_type

def form_val(key):
	"""
	Use this simple method to get http POST's
	"""
	val = form.getlist(key):
	if (val == None): return None
	return val

user_name = form_value('user_name')

def display_page(name):
	print CGI()
	print "<html><head><title>"\
		"This is %s's page</title>"\
		"</head><body><h1>Welcome, %s"\
		"</h1><body></html>" %(name, name)
	return 1	#

display_page(the_name)

----+

There are also other modules you can use to help produce your HTML with 
out typing it out for yourself and thus making it all more Pythonesque.

Hope this wasn't over the top and that it helps.

Mark d.



More information about the Python-list mailing list