Back to index

python-weblib  1.3.9
Functions | Variables
pyweblib.httphelper Namespace Reference

Functions

def DateTimeRFC1123
def SendHeader
def SimpleMsg
def URLRedirect

Variables

string __version__ = '0.0.5'
string HTTP_LINESEP = '\r\n'

Detailed Description

httphelper.py - basic HTTP-related functions
(c) by Michael Stroeder <michael@stroeder.com>

This module is distributed under the terms of the
GPL (GNU GENERAL PUBLIC LICENSE) Version 2
(see http://www.gnu.org/copyleft/gpl.html)

$Id: httphelper.py,v 1.13 2010/10/27 08:27:10 michael Exp $

Function Documentation

Return seconds as RFC1123 date/time format preferred
for HTTP 1.1 (see RFC2616)

Definition at line 18 of file httphelper.py.

00018 
00019 def DateTimeRFC1123(secs=0):
00020   """
00021   Return seconds as RFC1123 date/time format preferred
00022   for HTTP 1.1 (see RFC2616)
00023   """
00024   return time.strftime(
00025     '%a, %d %b %Y %H:%M:%S GMT',
00026     time.gmtime(secs)
00027   )
00028 
# Write HTTP-Header

Here is the call graph for this function:

Here is the caller graph for this function:

def pyweblib.httphelper.SendHeader (   outf = sys.stdout,
  contenttype = 'text/html',
  charset = 'ISO-8859-1',
  contentlength = None,
  expires_offset = 0,
  current_datetime = None,
  additional_header = None 
)
Generate HTTP header

outf
    File object used for sending to client.
contenttype
    MIME type of object in HTTP body. Default is 'text/html'.
charset
    Character set used. Default is 'ISO-8859-1'.
contentlength
    Content-Length if known and gzip-encoding is not used.
    Default is None indicating unknown length.
expires_offset=0,
    Expiry time from current time in seconds. Default is 0.
current_datetime
    Last modification time in seconds.
    If zero (default) 'Last-modified' header will be omitted.
additional_header
    Dictionary containing arbitrary additional HTTP header fields
    as key/value-pairs.

Definition at line 37 of file httphelper.py.

00037 
00038 ):
00039   """
00040   Generate HTTP header
00041 
00042   outf
00043       File object used for sending to client.
00044   contenttype
00045       MIME type of object in HTTP body. Default is 'text/html'.
00046   charset
00047       Character set used. Default is 'ISO-8859-1'.
00048   contentlength
00049       Content-Length if known and gzip-encoding is not used.
00050       Default is None indicating unknown length.
00051   expires_offset=0,
00052       Expiry time from current time in seconds. Default is 0.
00053   current_datetime
00054       Last modification time in seconds.
00055       If zero (default) 'Last-modified' header will be omitted.
00056   additional_header
00057       Dictionary containing arbitrary additional HTTP header fields
00058       as key/value-pairs.
00059   """
00060   additional_header = additional_header or {}
00061   gzip = hasattr(outf,'fileobj')
00062   # Get current time as GMT (seconds since epoch)
00063   gmt = time.time()
00064   # Determine times for HTTP header
00065   if current_datetime is None:
00066     current_datetime = DateTimeRFC1123(gmt)
00067   expires = DateTimeRFC1123(gmt+expires_offset)
00068   # Build list of header lines
00069   header_lines = []
00070   # Write header
00071   if not (contenttype is None):
00072     if contenttype.lower().startswith('text/'):
00073       header_lines.append('Content-Type: %s;charset=%s' % (contenttype,charset))
00074     else:
00075       header_lines.append('Content-Type: %s' % (contenttype))
00076   if not (contentlength is None):
00077     header_lines.append('Content-Length: %d' % (contentlength))
00078   if gzip:
00079     header_lines.append('Content-Encoding: gzip')
00080     header_lines.append('Vary: Accept-Encoding')
00081   header_lines.append('Date: %s' % (current_datetime))
00082   header_lines.append('Last-Modified: %s' % (current_datetime))
00083   header_lines.append('Expires: %s' % (expires))
00084   for h,v in additional_header.items():
00085     header_lines.append('%s: %s' % (h,v))
00086   # Write empty end-of-header line
00087   header_lines.extend(['',''])
00088   if gzip:
00089     outf.fileobj.write(HTTP_LINESEP.join(header_lines))
00090     outf.fileobj.flush()
00091   else:
00092     outf.write(HTTP_LINESEP.join(header_lines))
00093   return
00094 

Here is the call graph for this function:

Here is the caller graph for this function:

def pyweblib.httphelper.SimpleMsg (   outf,
  msg 
)
Output HTML text.

Definition at line 95 of file httphelper.py.

00095 
00096 def SimpleMsg(outf,msg):
00097   """
00098   Output HTML text.
00099   """
00100   SendHeader(outf)
00101   outf.write("""
00102 <html>
00103   <head>
00104     <title>Note</title>
00105   </head>
00106   <body>
00107     %s
00108   </body>
00109 </html>
00110     """ % (msg)
00111   )

Here is the call graph for this function:

def pyweblib.httphelper.URLRedirect (   outf,
  url,
  refreshtime = 0,
  msg = 'Redirecting...' 
)
Output HTML text with redirecting <head> section.

Definition at line 112 of file httphelper.py.

00112 
00113 def URLRedirect(outf,url,refreshtime=0,msg='Redirecting...'):
00114   """
00115   Output HTML text with redirecting <head> section.
00116   """
00117   SendHeader(outf)
00118   outf.write("""
00119 <html>
00120   <head>
00121     <meta http-equiv="refresh" content="%d; url=%s">
00122   </head>
00123   <body>
00124     %s <a href="%s">Redirecting...</a>
00125   </body>
00126 </html>
00127     """ % (refreshtime,url,msg,url)
00128   )

Here is the call graph for this function:


Variable Documentation

Definition at line 12 of file httphelper.py.

Definition at line 16 of file httphelper.py.