mailtb.py: made send_header() more flexible, added http_status dict
authorPascal Volk <user@localhost.localdomain.org>
Sat, 14 Nov 2009 06:01:06 +0000
changeset 2 731c43b63c9d
parent 1 bab77be235a8
child 3 7062c52d3999
mailtb.py: made send_header() more flexible, added http_status dict moved mail_error_file variable to mail_config['error_log']
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 """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">