More informative tracebacks [code]

Bjorn Pettersen BPettersen at
Fri Aug 30 18:35:53 CEST 2002

I just created the following code snippet to get tracebacks emailed back
to me when they happen in environments I don't have control over. I
thought it might be useful for someone...

Comments and improvements are welcome :-)

-- bjorn

import sys, cgitb
from cStringIO import StringIO

class EMailExcept(cgitb.Hook):
    def __init__(self, emailaddress):
        self.errout = StringIO() = emailaddress
        cgitb.Hook.__init__(self, 1, 'exlog', 5, self.errout)
    def handle(self, info=None):
        cgitb.Hook.handle(self, info)
        errtxt = self.errout.getvalue()
        import smtplib
        from email.Generator import Generator
        from email.MIMEText import MIMEText
        msg = MIMEText(errtxt, 'html')
        msg['Subject'] = 'Traceback from: ' + sys.argv[0]
        msg['From'] = 'Python'
        msg['To'] =
        out = StringIO()
        g = Generator(out)
        s = smtplib.SMTP()
        s.sendmail('Python', [], out.getvalue())
        print errtxt # to visually notify operators
        sys.exit(-1) # to signify failure if running in a batch system
sys.excepthook = EMailExcept('abuse at')

More information about the Python-list mailing list