Review of last class
In the last class, we talked about our first mod_python script. In order to configure mod_python to work on the Turing server, we need to add the following to our .htaccess file.
1. Login into your Turing account.
2. Make a directory called "public_html" if you have not done so already. "mkdir public_html"
3. Change to that directory. "cd public_html"
4. Start a new file called ".htaccess". "nano .htaccess"
5. Type the following directly as you see it below.
| .htaccess: |
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On |
6. Use CTRL-X to quit the program.
You should now be able to execute mod_python files from a web browser.
Points from last class:
- We need to import the apace library from the mod_python library. "from mod_python import apache"
- The default method that mod_python executes is called "index"
- The index method should return a single string variable containing an entire HTML-valid page.
- The method needs to have the first parameter be one called "req". This allows mod_python to pass environment variables into the script.
- When defining an HTML form, the "action" attribute of the form needs to point to the Python script used to process the inputs.
- When passing values from an HTML form to a Python script, the input names in the HTML form and the arguments to the index method need to have identical spellings.
- Data passed from the HTML from to the Python script is always in the String data type.
- Making a script be executable from both the command line and the web will help save you time debugging.
Splitting a script into multiple pages
mod_python, by default, executes the index method of a script. By changing the url of how the script is called, we can have mod_python execute a different page. This is a file called "spam.py". It has two hidden pages.
| spam.py: |
#!/usr/bin/env python
try:
from mod_python import apache
except ImportError:
pass
def index(req):
s = """<html>
<head><title>Hello</title></head>
<body>
<h1>This is my page dedicated to how much I love egg, bacon, sausage and spam.</h1>
</body>
</html>"""
return s
def its(req):
s = """<html>
<head><title>It's...</title></head>
<body>
<div style="width: 100%; text-align: center">
<img src="/~jcchurch/its.jpg" width="250" height="250"><br>
<h1>It's...</h1>
</div>
</body>
</html>"""
return s
def larch(req):
s = """<html>
<head><title>And now for something completely different.</title></head>
<body>
<div style="width: 100%; text-align: center">
<img src="/~jcchurch/larch.jpg" width="300" height="450"><br>
<h1>The Larch</h1>
</div>
</body>
</html>"""
return s |
To call each of these three pages, we can do this:
http://turing.cs.olemiss.edu/~jcchurch/spam.py
http://turing.cs.olemiss.edu/~jcchurch/spam.py/its
http://turing.cs.olemiss.edu/~jcchurch/spam.py/larch
Public and Private script components
Every thing in a script can be accessed using this "/element" manner. To make something private, we can use the "__element" like we did with classes to make it private.
Error Handling in Python Scripts
Use a try/except to catch a ValueError on attempts to convert strings to floats.
>>> decimal = "2.13"
>>> try:
... print float(decimal)
... except ValueError:
... print "'%s' is not a number!" % (decimal)
...
2.13
>>> word = "this is a string"
>>> try:
... print float(word)
... except ValueError:
... print "'%s' is not a number!" % (word)
...
'this is a string' is not a number!
>>>
Full Square Root Calculator Program
| sqrt.py: |
#!/usr/bin/env python
try:
from mod_python import apache
except ImportError:
pass
import math
def __take_square_root(num=""):
if num == "":
return "Error! Our value is blank."
try:
num = float(num)
if num < 0:
return "Error! Number is less than zero."
except ValueError:
return "Error! Value is not numerical."
return math.sqrt(num)
def index(req):
s = """<html>
<head><title>Square Root of Number</title></head>
<body>
<h1>Input a number to calculate square root</h1>
<form name="mysqrt" action="%s/process" method="POST">
<input type="text" name="num"><input type="submit">
</form>
</body>
</html>""" % ( req.uri )
return s
def process(req, num):
s = """<html>
<head><title>Square Root of Number</title></head>
<body>
<h1>%s</h1>
</body>
</html>""" % ( __take_square_root(num) )
return s |
Comments (0)
You don't have permission to comment on this page.