Merchako
Merchako

Reputation: 899

How can I troubleshoot my Python CGI script?

I have a Python CGI webapp that isn't working, and I don't know how to start narrowing down the problem. What can I do?


Note. I really appreciate the answer to this same question for Perl, and I wish I had one for Python. Some things similar, but most are different. That's my template for asking the question, so if you feel it should be asked differently, feel free to edit.

Upvotes: 4

Views: 2584

Answers (1)

Steven Kryskalla
Steven Kryskalla

Reputation: 14659

  1. Check the server's error logs. For example on Apache the path may be /var/log/apache/error.log or /var/log/httpd/error_log.

  2. Make sure your script is executable (chmod +x script.py).

  3. Make sure your script has a she-bang line (#!/usr/bin/env python).

  4. Some configurations only allow CGI scripts to execute if they're in a /cgi-bin/ directory, you can confirm by looking at the server's configuration or just try moving your script into that directory.

  5. Try running your script from the command line (./script.py). It should print something and exit successfully (you may need to set a few CGI environment variables if your script depends on them).

  6. Replace your script with a minimal script that just prints "Content-Type: text/plain\n\nHello world". This should tell you if CGI is working at all on the server.

  7. Put import cgitb; cgitb.enable() at the top of your file (first line below the shebang line) to capture any errors and show you the traceback. You should turn this off once you've solved the problem, because it can be a security risk to expose this information.

If you don't have access to the logs then you will need to ask the server's administrator for help, file a support ticket, etc. It's also helpful if you post what web server you're running under, what the web server configs look like, which python version, what your CGI script looks like, etc.

Upvotes: 5

Related Questions