# HG changeset patch # User Pascal Volk # Date 1258178466 0 # Node ID 731c43b63c9d9b51965e964e9a9b078b8dc31939 # Parent bab77be235a8524b4fe8c282b604b44a8ccfb01c mailtb.py: made send_header() more flexible, added http_status dict moved mail_error_file variable to mail_config['error_log'] diff -r bab77be235a8 -r 731c43b63c9d mailtb.py --- a/mailtb.py Sat Nov 14 02:30:57 2009 +0000 +++ b/mailtb.py Sat Nov 14 06:01:06 2009 +0000 @@ -18,8 +18,6 @@ from os import environ as env from time import ctime, gmtime, localtime, strftime, time -# make sure that the web server can write to this file -mail_error_file = '/tmp/mailtb_error.log' mail_config = {# sender information 'from_addr': '', 'from_name': '', @@ -34,28 +32,36 @@ 'smtp_port': 25, # 25 smtp, 587 submission, 465 ssmtp (obsolete) 'smtp_tls': False, # True or False (use starttls on port 25/587) # subject of the email - 'subject': 'Exception Notification' + 'subject': 'Exception Notification', + # make sure that the web server can write to this file + 'error_log': '/tmp/mailtb_error.log' } +http_status = {200: 'OK', + 500: 'Internal Server Error'} + def log_mail_error(error, timestamp): try: prefix = strftime('%b %d %H:%M:%S', localtime(timestamp)) line = error.message if len(error.message) else error.args - logfile = open(mail_error_file, 'a') + logfile = open(mail_config['error_log'], 'a') logfile.write('%s %s: %s\n' % (prefix, error.__class__.__name__, line)) logfile.close() except: pass -def send_header(): +def send_header(status=500, xhtml=False): + s = status if status in http_status else 500 write = sys.stdout.write - #write('Content-Type: text/html; charset=utf-8\r\n') - write('Content-Type: application/xhtml+xml; charset=utf-8\r\n') - write('Status: 500 Internal Server Error\r\n\r\n') + if not xhtml: + write('Content-Type: text/html; charset=utf-8\r\n') + else: + write('Content-Type: application/xhtml+xml; charset=utf-8\r\n') + write('Status: %d %s\r\n\r\n' % (s, http_status[s])) sys.stdout.flush() def send_error_page(): - send_header() + send_header(500, True) print """