6.13.1.6. HTTP input

This object fetches a file by http on construction. The file is then read from the local copy. The host name and remote filename arguments are mandatory. Optional arguments given by keywords are:
port
The port number of the host to use for FTP. Defaults to 21.
local_filename
The name of the file to which the download should be written. Defaults to 'remote_filename'.
refresh_interval
This is the number of days old the local file can be before ftp is done. The default is 28. If this value is set to 0, a download is always done. If the value is set to -1, a download is done only if the local file does not exist; delete the file to force another download.
An exception is thrown if there is no local file, and the attempt to ftp the remote file files. No exception is thrown if the local file exists, even if it is out of date.
Start python section to interscript/drivers/sources/http.py[1 /1 ]
     1: #line 584 "source_drivers.ipk"
     2: #---------------------------------------------------------
     3: # gets input by HTTP
     4: from interscript.drivers.sources.base import file_source
     5: from interscript.drivers.sources.base import eof
     6: import os
     7: import time
     8: import httplib
     9: import string
    10: 
    11: class http_file_source(file_source):
    12:   def __init__(self,host,remote_filename,encoding='utf8',**kwds):
    13:     appy(file_source.__init__, (self,encoding), kwds)
    14:     self.name = remote_filename
    15:     self.remote_filename = remote_filename
    16:     self.host = host
    17:     self.g = g
    18:     for k in kwds.keys():
    19:       self.__dict__[k]=kwds[k]
    20:     if not hasattr(self,'local_filename'):
    21:       self.local_filename = self.remote_filename
    22:     self.os = os
    23:     self.fetch()
    24:     self.file = open(self.local_filename,'r')
    25:     self.closed = 0
    26: 
    27:   def fetch(self):
    28:     if not hasattr(self,'refresh_interval'):
    29:       self.refresh_interval = 28
    30:     if self.refresh_interval < 0: self.refresh_interval = 100000
    31:     self.local_file_exists = 1
    32:     try:
    33:       f = open(self.local_filename)
    34:       f.close()
    35:       print 'local file',self.local_filename,'exists'
    36:     except:
    37:       print 'local file',self.local_filename,'does NOT exist'
    38:       self.local_file_exists = 0
    39: 
    40:     if self.local_file_exists:
    41:       self.local_file_modify_time = os.stat(self.local_filename)[stat.ST_MTIME]
    42:       now = time.time()
    43:       age = (now - self.local_file_modify_time)/ (24 * 60 * 60)
    44:       download = age > self.refresh_interval
    45:     else:
    46:       download = 1
    47: 
    48:     if hasattr(self.g,'download'):
    49:       if self.g.download == 'always': download = 1
    50:       if self.g.download == 'never': download = 0
    51: 
    52:     if download:
    53:       try:
    54:         print 'downloading',self.remote_filename
    55:         # create HTTP object
    56:         http = httplib.HTTP()
    57: 
    58:         # connect to server
    59:         if hasattr(self,'port'):
    60:           http.connect(self.host+':'+str(self.port))
    61:         else:
    62:           ftp.connect(self.host)
    63:         print 'connected to',self.host
    64: 
    65:         # set remote directory
    66:         to_download = self.remote_filename
    67:         if hasattr(self,'remote_directory'):
    68:           to_download = to_download + '/' + self.remote_directory
    69: 
    70:         # get file to a temporary
    71:         try:
    72:           http.putrequest('GET',to_download)
    73:           http.putheader('Accept','text/html')
    74:           http.putheader('Accept','text/plain')
    75:           http.endheaders()
    76:           errcode, errmsg, headers = http.getreply()
    77:           if errcode != 200: raise 'http error '+str(errcode)+'; '+errmsg
    78:           file = http.getfile()
    79:           newlines = file.readlines()
    80:           file.close()
    81:           print 'download complete'
    82: 
    83:           if self.local_file_exists:
    84:             file = open(self.local_filename,'r')
    85:             oldlines = file.readlines()
    86:             file.close()
    87: 
    88:             if newlines != oldlines:
    89:               print 'Local file',self.local_filename,'UPDATED from',self.remote_filename
    90:             else:
    91:               print 'Local file',self.local_filename,'unchanged'
    92:           else:
    93:             print 'Writing new local file',self.local_filename
    94: 
    95:           # note that the local file is written even if it isn't changed
    96:           # to update the time stamp
    97:           file = open(self.local_filename,'w')
    98:           file.writelines(newlines)
    99:           file.close()
   100: 
   101:         except:
   102:           print 'Cannot download',self.remote_filename,
   103:           if hasattr(self,'remote_directory'):
   104:             print 'from directory',self.remote_directory
   105:           else: print 'of',self.host
   106:           try:
   107:             print 'code',errcode,'msg',errmsg
   108:           except:
   109:             pass
   110:       except:
   111:         pass # ignore errors from ftp attempt
   112:     else:
   113:       print 'Skipping http download'
   114: 
   115:     self.file = open(self.local_filename,'r')
   116: 
End python section to interscript/drivers/sources/http.py[1]