apache 2 & python

K

Krzysztof Drozd

how to configure apache 2 to work with python or mod_python?



krzysiek

ps: sory,my english is't perfect :)
 
M

md

Krzysztof said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top