0

In this project I must use Python for a website, I'm looking for the errors to go in my browser instead of getting a 500 page. Any tip ?
Addition
I'm just using plain CGI nothing fancy I tried this as explained here but it does not work

#!/usr/bin/env python
print "Content-Type: text/plain"
print
import sys
sys.stderr = sys.stdout
f = open('non-existent-file.txt', 'r')
3
  • 4
    Are you using a web framework? If so, which? Commented Feb 22, 2013 at 19:32
  • 2
    Similarly, what server are you using? Commented Feb 22, 2013 at 19:33
  • I'm using Apache2 and no framework Commented Feb 22, 2013 at 22:00

2 Answers 2

2

It depends on your Python web framework settings.

For example, Pyramid framework has various setting for enabling traceback in web browser and start an interactive debugging session:

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/introduction.html#debugging-settings

It all depends on how your Python web application is wired up and what architecture it uses and so on. Please include full context information in the question for further help.

More information about debugging web applications with Python and WSGI:

http://code.google.com/p/modwsgi/wiki/DebuggingTechniques

If you are not using any framework you need to put everything inside a main level try...except block as Python standard library does not provide tools to convert exceptions to HTML.

  import traceback

  try:
       ...
  except Exception as e:
       # Set content type as text/plain (don't know how)
       traceback.print_exc(e)  # Print out exception as plain text
Sign up to request clarification or add additional context in comments.

2 Comments

apache2 and python3, no framework, also not WSGI but its on my plan to move to it
If you are plain Python with no framework, such functionality is not provided. You can write main level HTTP request handling function, which caches the exception and converts it to HTML page. More info about Python exception handling tools: docs.python.org/2/library/traceback.html
0

I ended up creating a script reading the errors in error.log. So instead of getting your errors in the same tab you have another tab for them. However don't forget to remove the script avoiding giving info about your system.

#! /opt/python3/bin/python3
#Error Log Viewer

import subprocess

p = subprocess.Popen('cat /var/log/apache2/error.log | tail -5', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

print ('Content-type: text/html\n\n') 
print (''' 
<!DOCTYPE html >
<html> <head></head> <body>''')
for line in p.stdout.readlines():
    print (line,'<br>')
print('''</html></body>''')

Comments

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.