Python is a programing language with a large following and some good... nevermind. I have enabled CGI on this server to run ".cgi" files. You can use Python in these files to have code run on the server side. This page describes how.
Each user has a deticated web directory on this machine... mine is
http://danm.ucsc.edu/~lyle yours is similar, using your login name.
example-cgi (but this is just for simplicity).Options ExecCGI Indexes
(The Indexes part allows a person to visit the directory and see a file listing).
Now that you have a directory that can run ".cgi" scripts you need to make a "Hello World" python cgi test script.
#!/usr/bin/python # print "Content-Type: text/html\n\n"; print "<html><body>Hello World</body></html>";
The first line tells the webserver what language this cgi script is written in. The third and forth line are sent back to the web browser when you visit the page.
Now you need to tell the webserver that this new text file should be executed on the serve... to do this you need to chagne the file permissions.
SSH into danm.ucsc.edu and change directory (cd) to the place you have your script and change the file "mode". Here is a sample of my ssh session.. I used Apple's Terminal application in /Applications/Utilities/
lyle:~ lyle$ ssh lyle@danm.ucsc.edu Password: I entered my password Last login: Fri Jan 13 16:03:40 2006 from lyle.ucsc.edu Welcome to Darwin! danm:~ lyle$ cd Sites/example-cgi/ danm:~/Sites/example-cgi lyle$ chmod a+x hello_world_with_python.cgi
Now your srcipt can run on the server... Here is my
hello_world_with_python.cgi running on the danm server.
Make a file (executable as last time) called "db_connect_python.cgi" with this contents:
#!/usr/bin/python
#
print "Content-Type: text/html\n\n"
import MySQLdb
conn = MySQLdb.connect (host = "localhost",
user = "danm220",
passwd = "thecorrectpassword",
db = "test")
cursor = conn.cursor ()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone ()
print "Content-Type: text/html\n\n"
print "server version:", row[0]
cursor.close ()
conn.close ()
When you run this (change the user, password, and perhaps the database) you should get the version of the MySQL database. You might notice that there is that print line that sends Content-Type: text/html and then two line feeds. If that is not sent back from a CGI script then the web server thinks something bad has happend and gives a generic error message. So that is really important. Here is my
db_connect_python.cgi script.
If you have an account on the danm server you can see my files before the web server executes them. Simply connect as a file share to danm.ucsc.edu and pick "Users" as the share point. Then navigate to lyle/Sites/example-cgi and check out what I have there.