Need help with first program to connect to mysql database via apache and python.

pythonbrian brianmarsh at yahoo.com
Wed Feb 6 20:23:35 EST 2008


I am just learning python and I am trying to create a simple
connection to a mysql table via Python and Apache, using a Python
program
Unfortunately I keep getting an internal server error (50), when I
bring it up in my browser ... information attached.
Any help would be appreciated ...
Thx, brianmarsh at yahoo.com

Information #1
error in /var/log/apache2/error.log

[Wed Feb 06 20:04:31 2008] [error] [client 127.0.0.1] (2)No such file
or directory: exec of '/var/www/cgi-bin/fig17_27.py' failed

[Wed Feb 06 20:04:31 2008] [error] [client 127.0.0.1] Premature end of
script headers: fig17_27.py


-----------------------------------------------------------------------
Information #2
directory information

brianmarsh at ubuntu:/var/log/apache2$ cd /var/www/cgi-bin

brianmarsh at ubuntu:/var/www/cgi-bin$ ls -al

total 24

drwxr-xr-x 2 root root 4096 2008-02-06 15:03 .

drwxr-xr-x 4 root root 4096 2008-02-02 20:53 ..

-rwxr-xr-x 1 root root 1569 2008-02-02 21:02 fig06_03.py

-rwxr-xr-x 1 root root 2067 2008-02-02 21:05 fig06_05.py

-rwxr-xr-x 1 root root 2031 2008-02-02 21:19 fig06_06.py

-rwxr-xr-x 1 root root 3489 2008-02-06 15:03 fig17_27.py


-----------------------------------------------------------------------------------------
Web Error

http://localhost/cgi-bin/fig17_27.py

Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.
Please contact the server administrator, webmaster at localhost and
inform them of the time the error occurred, and anything you might
have done that may have caused the error.
More information about this error may be available in the server error
log.

Apache/2.2.4 (Ubuntu) mod_python/3.3.1 Python/2.5.1 PHP/
5.2.3-1ubuntu6.3 Server at localhost Port 80

------------------------------------------------------------------------------
Program File
brianmarsh at ubuntu:/var/www/cgi-bin$ cat fig17_27.py


#!/usr/local/bin/python

# Fig. 17.27: fig17_27.py

# Displays contents of the Authors table,

# ordered by a specified field.



import MySQLdb

import cgi

import sys



def printHeader( title ):

   print """Content-type: text/html



<?xml version = "1.0" encoding = "UTF-8"?>

<!DOCTYPE html PUBLIC

   "-//W3C//DTD XHTML 1.0 Transitional//EN"

   "DTD/xhtml1-transitional.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml"

   xml:lang = "en" lang = "en">

<head><title>%s</title></head>



<body>""" % title



# obtain user query specifications

form = cgi.FieldStorage()



# get "sortBy" value

if form.has_key( "sortBy" ):

   sortBy = form[ "sortBy" ].value

else:

   sortBy = "firstName"



# get "sortOrder" value

if form.has_key( "sortOrder" ):

   sortOrder = form[ "sortOrder" ].value

else:

   sortOrder = "ASC"



printHeader( "Authors table from Books" )



# connect to database and retrieve a cursor

try:

   connection = MySQLdb.connect( db = "Books", user = "root" )





# error connecting to database

except MySQLdb.OperationalError, error:

   print "Error:", error

   sys.exit( 1 )



# retrieve cursor

else:

   cursor = connection.cursor()



# query all records from Authors table

cursor.execute( "SELECT * FROM Authors ORDER BY %s %s" %

   ( sortBy, sortOrder ) )



allFields = cursor.description  # get field names

allRecords = cursor.fetchall()  # get records



# close cursor and connection

cursor.close()

connection.close()



# output results in a table

print """\n<table border = "1" cellpadding = "3" >

      <tr bgcolor = "silver" >"""



# create table header

for field in allFields:

   print "<td>%s</td>" % field[ 0 ]



print "</tr>"



# display each record as a row

for author in allRecords:

   print "<tr>"



   for item in author:

      print "<td>%s</td>" % item



   print "</tr>"



print "</table>"



# obtain sorting method from user

print """

      \n<form method = "post" action = "/cgi-bin/fig17_27.py">

      Sort By:<br />"""



# display sorting options

for field in allFields:

   print """<input type = "radio" name = "sortBy"

      value = "%s" />""" % field[ 0 ]

   print field[ 0 ]

   print "<br />"



print """<br />\nSort Order:<br />

      <input type = "radio" name = "sortOrder"

      value = "ASC" checked = "checked" />

      Ascending

      <input type = "radio" name = "sortOrder"

      value = "DESC" />

      Descending

      <br /><br />\n<input type = "submit" value = "SORT" />

      </form>\n\n</body>\n</html>"""



More information about the Python-list mailing list