如何在PHPlike mann中显示python web应用程序异常

2024-09-30 07:22:40 发布

您现在位置:Python中文网/ 问答频道 /正文

在这个项目中,我必须为一个网站使用Python,我正在寻找错误去我的浏览器,而不是得到一个500页。有小费吗?
添加 我只是用普通的CGI没什么特别的 我试过这样做here但它不起作用

#!/usr/bin/env python
print "Content-Type: text/plain"
print
import sys
sys.stderr = sys.stdout
f = open('non-existent-file.txt', 'r')

Tags: 项目textenvbinhere网站usrtype
2条回答

这取决于您的pythonweb框架设置。你知道吗

例如,Pyramid framework具有各种设置,用于在web浏览器中启用回溯并启动交互式调试会话:

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

这完全取决于pythonweb应用程序的连接方式以及它使用的体系结构等等。请在问题中包含完整的上下文信息以获得进一步帮助。你知道吗

有关使用Python和WSGI调试web应用程序的详细信息:

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

如果您不使用任何框架,您需要将所有内容放在主级别中,请尝试…除了block,因为Python标准库不提供将异常转换为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

我最终创建了一个脚本来读取错误.log. 所以你不用把你的错误放在同一个标签上,而是换了一个标签。但是,不要忘记删除脚本,避免提供有关系统的信息。你知道吗

#! /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>''')

相关问题 更多 >

    热门问题