Oct 01
HOWTO: apache2 & mod_python
Here are two mod_python howtos I wrote, hope they are helpful in any way:
Part 1: mod_pythton.publisher section:
After a bit of research and some debugging time, and as i couldn’t find obvious mod_python howtos, I decided to write a little Apache2/mod_python howto.
The first thing to do, of course, is to setup your web server to handle python code.
To do so, you must edit your site’s apache2 configuration file. In this example, we will edit the “default” site of apache2.
As root, add the following to your configuration file:
vi /etc/apache2/sites-available/default
and add:
ServerAdmin webmaster@localhost DocumentRoot /path/to/your/sites/files AddHandler mod_python .py PythonHandler mod_python.publisher PythonDebug On ...
When this is done, you must restart apache2 to reload your new configuration. (sudo /etx/init.d/apache2 restart)
- Hello world!
- First HTML:
- Create a function (other than index() ) and call it:
- HTML POST method with mod_python:
- Import a customized module in your mod_python code (from my last post):
- One last thing..
- Conclusion:
The first thing to do to test your configuration, is to create a “hello wolrd!” page : )
Go to your site’s file path (by default /var/www/), create a file named hello.py and paste in it the following:
def index(): return "Hello world!"
Now try it: http://server_ip/hello.py
This should just give you a text output embedded in to the page. No HTML source code is present.
WARNING:
As you can test, when loading a python based page, the method that is looked for by default is “index()”.
So, if your try changing the name of the index definition into bloup, this shouldn’t work!
The requested URL /hello.py was not found on this server.
We’ll see how to get over this in part 3.
Now let’s write our first HTML page. This page will ,of course, have dynamic content, else, you should just code in HTML without bothering to do it through mod_python. : )
What we’ll do is import some standard python modules, and use some of their output in our page. For instance, the time and date of the server. For that we will import the time module:
import time def index(): html = """ <html><head> <title>mod_python.publisher first html page</title> </head> <body> <h1>This page was generated by mod_python.publisher</h1><hr> The local time of this server is: %sThe timezone of this server is : %s </body> </html> """ % (time.ctime(time.time()), time.timezone/3600) return html
This should return something like:
This page was generated by mod_python.publisher
The local time of this server is: Fri Sep 28 16:27:38 2007
The timezone of this server is : -1 GMT
In the last parts we only called the .py file through our browser, which will call the index() function by default.
To get the output of a other function, get_time() for example, you must enter: http://server_ip/hello.py/get_time
import time def index(): return "index().. nothing here.." def get_time(): html = """ <html><head> <title>get_time function</title> </head> <body> <h1>get_time function</h1> <hr> The local time of this server is: %s <br> The timezone of this server is : %s <br> </body> </html>""" % (time.ctime(time.time()), time.timezone/3600) return html
Let’s see how to post some info through a “submit” button, and use it in mod_python function.
First of all, lets create a simple html post:
def index(): return """ <html><head> <title>get_time function</title> </head> <body> <FORM value="form" action="hello.py/get_info" method="post"> <P> <LABEL for="firstname">First name: </LABEL> <INPUT type="text" name="firstname"><BR> <LABEL for="lastname">Last name: </LABEL> <INPUT type="text" name="lastname"><BR> <LABEL for="email">email: </LABEL> <INPUT type="text" name="email"><BR> <INPUT type="radio" name="gender" value="Male"> Male<BR> <INPUT type="radio" name="gender" value="Female"> Female<BR> <INPUT type="submit" value="Send"> <INPUT type="reset"> </P> </FORM> </body> </html> """
Now let’s create the get_info method to gather the info posted from the form:
def get_info(req): info = req.form a = info['firstname'] b = info['lastname'] c = info['email'] d = info['gender'] return “”" <html><head> <title>POST method with mod_python</title> </head> <body> <h1>POST method with mod_python</h1> <hr><br> Your firstname is: %s <br> Your lastname is: %s <br> Your email is: %s <br> You are a %s <br> </body> </html> “”" %(a,b.upper(),c,d.lower())
To test it, goto http://server_ip/hello.py, enter the required information in the form and submit.
This should generate a html output with the info you have just entered.
In this example, we will try to import the module bloup.py located in the same directory of my index.py mainpage.
when trying to import using the usual means, we get:
#!/usr/bin/env python import os import bloup ... >>>ImportError: No module named bloup
The solution I found was:
import os
from mod_python import apache
directory = os.path.dirname(__file__)
bloup = apache.import_module('bloup', path=[directory])
You can checkout my source: http://www.dscpl.com.au/wiki/ModPython/Articles/BasicsOfModuleImporting
Now one last thing: posting variables through the address bar.
Let’s say we have a simple “bloup” variable in the index method: index(bloup)
You would just have to call the script this way: http://server_ip/hello.py?bloup=”Hello World!”
def index(bloup=""): return """<html><head> <title>get_time function</title> </head> <body> <h1> %s </h1> </body> </html> """ % bloup
I hope this was helpful. If you have any comment or tips, post it!
I’ll be glad to help or be helped..: )
Cheers,
Ghantoos
Part 2: mod_pythton.psp section:
Here is a second howto about the PSP handler of mod_python.
Hope this is useful!
The PSP handler of mod_python is what you need to use to have python code embeded in your page (more or less like with php).
The first thing to do, of course, is to setup your web server to handle python code.
To do so, you must edit your site’s apache2 configuration file. In this example, we will edit the “default” site of apache2.
As root, add the following to your configuration file:
vi /etc/apache2/sites-available/default
and add:
ServerAdmin webmaster@localhost DocumentRoot /path/to/your/sites/files AddHandler mod_python .py PythonHandler mod_python.psp PythonDebug On ...
When this is done, you must restart apache2 to reload your new configuration. (sudo /etx/init.d/apache2 restart)
- Hello world!
- First HTML:
- HTML POST method with mod_python:
- Import a customized module in your mod_python code (from my last post):
- Conclusion:
The first thing to do to test your configuration, is to create a “hello wolrd!” page : )
Go to your site’s file path (by default /var/www/), create a file named hello.py and paste in it the following:
<%= 'Hello World!' %>
Now try it: http://server_ip/hello.psp
This should just give you a text output embedded in to the page. No HTML source code is present.
WARNING:
As you can test, when loading a python based page, the method that is looked for by default is “index()”.
So, if your try changing the name of the index definition into bloup, this shouldn’t work!
The requested URL /hello.py was not found on this server.
We’ll see how to get over this in part 3.
Now let’s write our first HTML page. This page will ,of course, have dynamic content, else, you should just code in HTML without bothering to do it through mod_python. : )
What we’ll do is import some standard python modules, and use some of their output in our page. For instance, the time and date of the server. For that we will import the time module:
<html><head> <title>mod_python.psp first html page</title> </head> <body> <% import time %> <h1>This page was generated by mod_python.psp</h1><hr> The local time of this server is: <%= time.ctime(time.time()) %> The timezone of this server is : <%= time.timezone/3600 %> </body> </html>
This should return something like:
This page was generated by mod_python.psp
The local time of this server is: Fri Sep 28 16:27:38 2007
The timezone of this server is : -1 GMT
Let’s see how to post some info through a “submit” button, and use it in mod_python function.
First of all, lets create a simple html post:
<html><head>
<title>mod_python.psp Forms</title>
</head>
<body>
<%
if form.has_key('firstname'):
req.write("<pre>")
req.write("Your firstname is: " + form['firstname'] + “<br>”)
req.write(”Your lastname is: ” + form['lastname'].upper() + “<br>”)
req.write(”Your email is: ” + form['email'] + “<br>”)
req.write(”You are a ” + form['gender'] + “<br>”)
req.write(”</pre>”)
else:
%>
<FORM value=”form” action=”hello.psp” method=”post”>
<P>
<LABEL for=”firstname”>First name: </LABEL>
<INPUT type=”text” name=”firstname”><BR>
<LABEL for=”lastname”>Last name: </LABEL>
<INPUT type=”text” name=”lastname”><BR>
<LABEL for=”email”>email: </LABEL>
<INPUT type=”text” name=”email”><BR>
<INPUT type=”radio” name=”gender” value=”Male”> Male<BR>
<INPUT type=”radio” name=”gender” value=”Female”> Female<BR>
<INPUT type=”submit” value=”Send”> <INPUT type=”reset”>
</P>
</FORM>
</body>
</html>
To test it, goto http://server_ip/hello.psp, enter the required information in the form and submit.
This should generate a html output with the info you have just entered.
In this example, we will try to import the module EXIF.py located in the same directory of my index.py mainpage.
when trying to import using the usual means, we get:
<% import os import EXIF ... %> >>>ImportError: No module named bloup
The solution I found was:
import os
from mod_python import apache
bloup = apache.import_module('bloup', path=['/path/to/py_module/file/'])
You can checkout my source (for mod_python.publisher): http://www.dscpl.com.au/wiki/ModPython/Articles/BasicsOfModuleImporting
I didn’t find how to post a variable through the address bar..
I feel like the psp handler of mod_python is nicer to read, the publisher has a charm I like.
Again, I hope this was helpful. And again, if you have any comment or tips, post it!
I’ll be glad to help or be helped..: )
Cheers,
Ghantoos

October 1st, 2007 at 1:36 pm
[...] HOWTO: apache2 & mod_python [...]