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
    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,

    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@ubuntu:/var/log/apache2$ cd /var/www/cgi-bin

    brianmarsh@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@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@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>"""
    pythonbrian, Feb 7, 2008
    #1
    1. Advertising

  2. pythonbrian

    Steve Holden Guest

    Re: Need help with first program to connect to mysql database viaapache and python.

    pythonbrian wrote:
    > 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,
    >
    > 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@ubuntu:/var/log/apache2$ cd /var/www/cgi-bin
    >
    > brianmarsh@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@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@ubuntu:/var/www/cgi-bin$ cat fig17_27.py


    #!/usr/local/bin/python


    [...]

    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.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Steve Holden, Feb 7, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Allen Marshall
    Replies:
    1
    Views:
    191
    Gunnar Hjalmarsson
    Jan 24, 2004
  2. Replies:
    2
    Views:
    278
  3. Mohsen Pahlevanzadeh

    Qt connect and first connect or unicode

    Mohsen Pahlevanzadeh, Sep 17, 2013, in forum: Python
    Replies:
    3
    Views:
    152
    Mohsen Pahlevanzadeh
    Sep 18, 2013
  4. Mohsen Pahlevanzadeh

    Re: Qt connect and first connect or unicode

    Mohsen Pahlevanzadeh, Sep 17, 2013, in forum: Python
    Replies:
    0
    Views:
    114
    Mohsen Pahlevanzadeh
    Sep 17, 2013
  5. Oscar Benjamin

    Re: Qt connect and first connect or unicode

    Oscar Benjamin, Sep 17, 2013, in forum: Python
    Replies:
    0
    Views:
    113
    Oscar Benjamin
    Sep 17, 2013
Loading...

Share This Page