.cgi web server problem
#!/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 on my web server. I run it at 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 response
- test.cgi is executable (I run $ chmod + x test.cgi)
- I am using Apache.
- I have this in .bashrc export PATH = $ {PATH}: ~ / bin
- Python MySQLdb module installed.
- There are no smart quotes in the code.
[edit] after the second answer
you get this error because you have not installed the MySQLdb module that Python should talk to the MySQL database
I installed MySQLdb on my system. The module works, since I can import them. However, I still get the same error as on www. [Mysite] .com / test.cgi.
[edit]
I'm not sure about the issues
connect()? MySQL ?
MySQL . connect()?
SELECT?
, SQL, SELECT? SQL-. ?
, :
-
test.cgi(chmod 755) ? - ,
test.cgi, ScriptAlias, ExecCGI ( , Apache)? -
pythonPATHPATH-? - MySQLdb Python?
-
connect()? MySQL ? -
SELECT?
, python ( script , -, which python), , script shebang:
import cgitb
cgitb.enable()
: http://docs.python.org/library/cgitb.html
, -, python :
>>> import MySQLdb
, # 4 . , MySQLdb Python -.
: , , Django. , № 5 , , , , , , MySQL, -.
, - " '. ? , 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()
The error http://dpaste.com/8866/ occurs because you use "curly quotes" instead of standard ASCII quotes.
You want to replace "and" with "Just use search and replace in a text editor.