0

I'm new to cgi and python, so I've been making quite a few mistakes. The problem is that if something goes wrong with the script, I just get a 500 error on the webpage. The only way I can see what caused the error is by executing the page via ssh, but the page involves file uploads, so I can't test that part.

Is there a way I can output Python errors to a file?

1
  • What's wrong with viewing the log files on the server? Commented Apr 20, 2012 at 21:39

2 Answers 2

3

there are a couple of options, use the logging module as directed, you can tail the server's error log, and you can enable cgitb with import cgitb; cgitb.enable()

Depending on exactly where the error occurs, the error will show up in different places, so checking all three, and using print statements and exception blocks helps to debug your code.

With file uploads, I've found I have to explicitly state enctype="multipart/form-data" in the form tag or it breaks, often quietly.

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

3 Comments

I'm still not getting anything except the 500
those should definitely show up in the server error log with some infornation. generally i find i forgot to pass the print """Content-type: text/html""" or there is a syntax error, if the former, python might not raise any sort of exception, if the latter, it will show in the log like ` File "/usr/share/bdot/cgi/call", line 15 status, junk = commands.getstatusoutput('ssh [email protected] pwd ^ SyntaxError: EOL while scanning string literal`
Wow, I'm retarded. Filezilla had changed the permissions, so I just had to change them back. Once I did, I purposely broke the code to test this and it worked, so I'll try it next time. Thanks so much!
1

Yes there is. Python has a logging module which allows you to do just what you want. Here you can read the documentation, it is so easy to understand.

3 Comments

It's still not printing anything to the file when the error occurs. From ssh, the messages from the tutorial go into the file, but when I try the page from the browser, I still just get the 500 error and nothing goes in the file.
Well, it is not magical. You need to log the error using the logging module with the message you want to see (maybe a brief description of the exception, maybe the full trace of the error). By example: try: execute_function(self) except Exception, e: logging.error('I found an error my Lord: \n\t|%s', e)
I set logging at the top of the page and nothing was logged.

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.