Using Python
What is Python?
Python is an interpreted, interactive, object-oriented programming language. Primus' UNIX VWS customers can use Python to deliver dynamic content on their websites.
How do I use Python?
Python scripts are plain text files containing Python code. Your phython scripts should be stored in your cgi-bin directory and should have executable permissions. The very first line of your script should contain the following line:
#! /usr/local/bin/python
Which additional Python libraries are installed on the server?
The base Python distribution has been installed on the server. In addition, the mySQL libraries for Python have been installed on the server to allow you to connect to your mySQL database.
Sample Script #1 - Hello World
This script outputs the text string "Hello World!"
#! /usr/local/bin/python
print "Content-type text/html\n\n"
print "Hello World!"
Note:
Unlike other programming languages, you do not place a semicolon at the end of each line of code.
Sample Script #2 - HTTP Environment Variables
This script will display the http environment variables.
#! /usr/local/bin/python
import os
print "Content-type text/html\n\n"
print "Python script output of environment variables\n<BR>"
print "<BODY BGCOLOR=#FFFFFF><TABLE>"
for key in os.environ.keys():
value = os.environ.get(key)
print '<TR><TD>'+ key + ' </TD><TD> ' + value + '</TD></TR>'
print "</TABLE></BODY>"
In the above script, you can see that there is no clear end for the loop other than the tab indentation within the loop. Python uses tabs to delimit the scope of each block of code.
Sample Script #3 - Form Handling
Python can be used to handle CGI forms and process form results.
#! /usr/local/bin/python
import cgi
print "Content-Typ: text/plain\n\n"
The_Form = cgi.FieldStorage()
print "<BODY BGCOLOR=#FFFFFF><TABLE>"
if The_Form.keys():
for name in The_Form.keys():
print "<TR><TD>Input:</TD><TD>" + name + \
"</TD><TD>value:</TD><TD>" + The_Form[name].value + \
"<TD></TD></TR>"
else:
print "<TR><TD>No elements are passed to the form</TD></TR>"
print "</TABLE></BODY></HTML>"
Sample Script #4 - Database Access
Python can be used to access a Mysql database on the server.
#! /usr/local/bin/python
import MySQLdb
# Database connection.
dbh = MySQLdb.connect(host='localhost',db='dbname',user='user',passwd='passwd')
handle = dbh.cursor()
query = "SELECT name,id FROM DOMAINS"
handle.execute(query)
# fetch all rows into the Results array
Results = handle.fetchall()
# find out how many records were returned
total = len(Results)
print "Content-type text/html\n\n"
print "Python script output of mysql database content\n<BR>";
print "<BODY BGCOLOR=#FFFFFF><TABLE>"
array_container = []
if total < 1:
print "<TR><TD>No entries found </TD><TR>\n"
else:
for record in range(total):
# a blank dictionary to hold each record
entry = {}
# field 0 = name of the domain
entry["name"] = Results[record][0]
# field 1 = id of domain
entry["id"] = Results[record][1]
# add this entry to the master list
array_container.append(entry)
# we'll pretend we set up an HTML table here...
## parse variables into table for entry in array_container:
print "<TR><TD>" + entry["name"] + "</TD><TD>" str(entry["id"]) + "</TD></TR>\n"
print "</TABLE></BODY>"
Python Documentation
Hopefully this will give you enough pointers to get started with Python to deliver dynamic content. For more information, refer to the following resources.
http://www.python.org
http://www.devshed.com/Server_Side/Python
news:comp.lang.python
Troubleshooting
Primus cannot provide general technical support for Python development, but we will support you if the problem is potentially with how Primus has set up Python on the server. Send all problem reports to .