tags:

views:

172

answers:

4

The code

#!/usr/bin/env python

import MySQLdb

print "Content-Type: text/html"
print 
print "<html><head><title>Books</title></head>"
print "<body>" print "<h1>Books</h1>" 
print "<ul>"

connection = MySQLdb.connect(user='me', passwd='letmein', db='my_db') cursor = connection.cursor() cursor.execute(“SELECT name FROM books ORDER BY pub_date DESC LIMIT 10”)

for row in cursor.fetchall():
    print "<li>%s</li>" % row[0]

print "</ul>" 
print "</body></html>"

connection.close()

I saved it as test.cgi to my web server. I run it by www.mysite.com/test.cgi unsuccessfully

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

How can you solve the problem?

[edit] after the first answer

  1. test.cgi is executable (I run $ chmod +x test.cgi)
  2. I use Apache.
  3. I have this in .bashrc export PATH=${PATH}:~/bin
  4. Python module MySQLdb is installed.
  5. The code does not have smart quotes.

[edit] after the second answer

you're getting that error because you haven't installed the MySQLdb module that Python needs to talk to a MySQL database

I installed MySQLdb to my system. The module works, since I can import them. However, I still get the same error whet I go to the www.[mysite].com/test.cgi.

[edit]

I am not sure about the questions

Are the connect() parameters correct? Is MySQL running on localhost at the default port?

I run MySQL on my server. Is the question about the connect() parameters relevant here?

Is the SELECT statement correct?

You mean whether I have my SQL statements such as SELECT statement correct? I have not used any SQL queries yet. Do I need them here?

+2  A: 

I've tidied up the code a bit by inserting linebreaks where necessary and replacing smart quotes with " and '. Do you have any more luck with the following? Can you run it from a terminal just by typing python test.cgi?

#!/usr/bin/env python

import MySQLdb

print "Content-Type: text/html"
print
print "<html><head><title>Books</title></head>"
print "<body>"
print "<h1>Books</h1>"
print "<ul>"

connection = MySQLdb.connect(user='me', passwd='letmein', db='my_db')
cursor = connection.cursor()
cursor.execute("SELECT name FROM books ORDER BY pub_date DESC LIMIT 10")

for row in cursor.fetchall():
    print "<li>%s</li>" % row[0]

print "</ul>"
print "</body></html>"

connection.close()
Pourquoi Litytestdata
Thank you! I put your suggestions to the question.
Masi
I get the following error message when I run it in terminal: http://dpaste.com/8866/
Masi
@Masi: as others have said, you've got that error message because the code has smart quotes in it. What error messages (if any) do you get if you run the Python code in my answer?
Pourquoi Litytestdata
Pourquoi: The error in browser is the same, while in Terminal the error message changed http://dpaste.com/8977/
Masi
@Masi: you're getting that error because you haven't installed the MySQLdb module that Python needs to talk to a MySQL database. Download it from http://sourceforge.net/projects/mysql-python .
Pourquoi Litytestdata
+2  A: 

Any number of issues can cause the error you are seeing:

  1. Is test.cgi executable (chmod 755) on the server?
  2. Is the directory in which you placed test.cgi designated as a ScriptAlias location or have the ExecCGI option enabled (or equivalent if you're not using Apache)?
  3. Is python in the system PATH or in the PATH in the Web server's startup environment?
  4. Is the MySQLdb Python library installed?
  5. Are the connect() parameters correct? Is MySQL running on localhost at the default port?
  6. Is the SELECT statement correct?

If you're sure that python is found (test using the simplest possible script or by logging into the Web server if you can and typing which python) then you can get much better debug output by adding the following to the top of your script just below the shebang:

import cgitb
cgitb.enable()

More details: http://docs.python.org/library/cgitb.html

Additionally, if you have shell access to the Web server, try running python and just typing:

>>> import MySQLdb

If the command returns with no error, you have your answer for #4 above. If an error is printed, you will need to get MySQLdb installed into the Web server's Python installation.

EDIT: Looking more closely at the top of your question, I see that the code was scraped from an illustrative example at the very beginning of the Django Book. As such, I might expand #5 above to include the caveat that, of course, the requisite database, tables, user, and permissions need to be set up on the MySQL installation available to the Web server.

David Citron
Answers: 1. file is executable (I run $ chmod +x test.cgi 2. I am not yet sure about (2). I use Apache. 3. I have this in .bashrc export PATH=${PATH}:~/bin 4. not yet 5. not sure about the question 6. I have not yet run any SQL commands. Could you clarify (5) and (6), please.
Masi
@Masi: if MySQLdb is not installed then the Python script will fail on the very first line ("import MySQLdb"). When it fails, it will abort and therefore not return any data--even headers--to Apache. Hence the error you see.
David Citron
@Masi: Please add those "cgitb" lines just below the shebang--then you'll get some nice errors displayed in your browser
David Citron
@David: I did it, but the error message did not change. My code is now http://dpaste.com/8860/
Masi
I get the following error when I run the code in my server's terminal: http://dpaste.com/8866/
Masi
@Masi: Ahhh, I see. So your first problem is that the code you copied has "smart quotes" instead of the ASCII characters ' and " -- this is what Pourquoi was referring to in another answer to this question.Please type over all of your quotes or use Pourquoi's version.
David Citron
+1  A: 

The error in http://dpaste.com/8866/ is occurring because you are using "curly quotes" instead of standard ASCII quotation marks.

You'll want to replace the “ and ” with ". Just use find and replace in your text editor.

Matthew Christensen
A: 

Make sure you're saving the file with correct line endings. i.e. LF only on unix, or CR/LF for Windows. I just recently had the exact same problem...

Therms
I am using Vim. How can I check that the line endings are LF? I find that LF is U+000A. (source http://en.wikipedia.org/wiki/Newline)
Masi