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

Discussion in 'Python' started by pythonbrian, Feb 7, 2008.

  1. pythonbrian

    pythonbrian Guest

    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
    Unfortunately I keep getting an internal server error (50), when I
    bring it up in my browser ... information attached.
    Any help would be appreciated ...

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

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

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

    Information #2
    directory information

    [email protected]:/var/log/apache2$ cd /var/www/cgi-bin

    [email protected]:/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

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

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

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

    Web Error


    Internal Server Error
    The server encountered an internal error or misconfiguration and was
    unable to complete your request.
    Please contact the server administrator, [email protected] 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

    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
    [email protected]:/var/www/cgi-bin$ cat


    # Fig. 17.27:

    # 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"?>


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


    <html xmlns = ""

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


    <body>""" % title

    # obtain user query specifications

    form = cgi.FieldStorage()

    # get "sortBy" value

    if form.has_key( "sortBy" ):

    sortBy = form[ "sortBy" ].value


    sortBy = "firstName"

    # get "sortOrder" value

    if form.has_key( "sortOrder" ):

    sortOrder = form[ "sortOrder" ].value


    sortOrder = "ASC"

    printHeader( "Authors table from Books" )

    # connect to database and retrieve a cursor


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

    # error connecting to database

    except MySQLdb.OperationalError, error:

    print "Error:", error

    sys.exit( 1 )

    # retrieve cursor


    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



    # 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/">

    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" />


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

    value = "DESC" />


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

    pythonbrian, Feb 7, 2008
    1. Advertisements

  2. pythonbrian

    Steve Holden Guest

    [email protected]:/var/www/cgi-bin$ cat



    I've snipped the code, because the error message indicates it isn't
    being executed: your problem is more fundamental than a Python error.

    I take it you do have an executable /usr/local/bin/python?

    One thing that can trip you up is if you have carriage returns in tour
    script: then Apache fails to find the program from the shebang line
    because it takes the DR before the LF as part of the filename.

    Steve Holden, Feb 7, 2008
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.