0

I'm having some trouble with MySQLdb when using it in a CGI script. I'm on OSx and running off a local host.

Basically, when I run my code from the terminal it runs fine, but when I run it from a browser, it executes everything up until the import MySQLdb line, and nothing after.

For example:

#!/usr/bin/python 
print "Content-Type: text/html\n" 
print "abc"

In the browser and the terminal, this prints out abc.

But the following:

#!/usr/bin/python 
print "Content-Type: text/html\n" 
print "abc"
import MySQLdb 
print "123"

In the terminal prints out abc and 123, but in the browser it just prints out abc.

It seems that it's having a problem with the import MySQLdb line, but I can't see any errors.

Would anyone have any ideas?

1 Answer 1

1

To narrow down the problem, I'd look in two places. The first would be your webserver's error log. I'm not sure where that is on OS X, but on e.g. Red Hat it can be found in /var/log/httpd/error_log. Use tail -f to watch the error log as you submit your HTTP request:

$ tail -f /var/log/httpd/error_log

The other thing you can look into is Python's cgi and cgitb modules. The former will save you having to reinvent wheels, and the latter will give you tracebacks in your browser.

Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant! I checked the error log and it suggested I didn't have access to a folder, so I just did sudo chmod 777 /Library/Webserver. Thanks!
Glad I could help! One word of warning: it's probably OK for your local machine to chmod 777, but when you go into production, it's best to sort out exactly what user needs what permissions and use more restrictive permissions. If you don't, you could end up with security issues on your hands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.