6.3.1.6. HTTP input

6.3.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: #line 418 "source_drivers.ipk"
     2: #---------------------------------------------------------
     3: # gets input by HTTP
     4: from interscript.drivers.sources.base import source
     5: from interscript.drivers.sources.base import eof
     6: import os
     7: import time
     8: import httplib
     9: 
    10: class http_file_source(source):
    11:   def __init__(self,host,remote_filename,**kwds):
    12:     source.__init__(self)
    13:     self.name = remote_filename
    14:     self.remote_filename = remote_filename
    15:     self.host = host
    16:     self.g = g
    17:     for k in kwds.keys():
    18:       self.__dict__[k]=kwds[k]
    19:     if not hasattr(self,'local_filename'):
    20:       self.local_filename = self.remote_filename
    21:     self.os = os
    22:     self.fetch()
    23:     self.file = open(self.local_filename,'r')
    24:     self.closed = 0
    25: 
    26:   def fetch(self):
    27:     if not hasattr(self,'refresh_interval'):
    28:       self.refresh_interval = 28
    29:     if self.refresh_interval < 0: self.refresh_interval = 100000
    30:     self.local_file_exists = 1
    31:     try:
    32:       f = open(self.local_filename)
    33:       f.close()
    34:       print 'local file',self.local_filename,'exists'
    35:     except:
    36:       print 'local file',self.local_filename,'does NOT exist'
    37:       self.local_file_exists = 0
    38: 
    39:     if self.local_file_exists:
    40:       self.local_file_modify_time = os.stat(self.local_filename)[stat.ST_MTIME]
    41:       now = time.time()
    42:       age = (now - self.local_file_modify_time)/ (24 * 60 * 60)
    43:       download = age > self.refresh_interval
    44:     else:
    45:       download = 1
    46: 
    47:     if hasattr(self.g,'download'):
    48:       if self.g.download == 'always': download = 1
    49:       if self.g.download == 'never': download = 0
    50: 
    51:     if download:
    52:       try:
    53:         print 'downloading',self.remote_filename
    54:         # create HTTP object
    55:         http = httplib.HTTP()
    56: 
    57:         # connect to server
    58:         if hasattr(self,'port'):
    59:           http.connect(self.host+':'+str(self.port))
    60:         else:
    61:           ftp.connect(self.host)
    62:         print 'connected to',self.host
    63: 
    64:         # set remote directory
    65:         to_download = self.remote_filename
    66:         if hasattr(self,'remote_directory'):
    67:           to_download = to_download + '/' + self.remote_directory
    68: 
    69:         # get file to a temporary
    70:         try:
    71:           http.putrequest('GET',to_download)
    72:           http.putheader('Accept','text/html')
    73:           http.putheader('Accept','text/plain')
    74:           http.endheaders()
    75:           errcode, errmsg, headers = http.getreply()
    76:           if errcode != 200: raise 'http error '+str(errcode)+'; '+errmsg
    77:           file = http.getfile()
    78:           newlines = file.readlines()
    79:           file.close()
    80:           print 'download complete'
    81: 
    82:           if self.local_file_exists:
    83:             file = open(self.local_filename,'r')
    84:             oldlines = file.readlines()
    85:             file.close()
    86: 
    87:             if newlines != oldlines:
    88:               print 'Local file',self.local_filename,'UPDATED from',self.remote_filename
    89:             else:
    90:               print 'Local file',self.local_filename,'unchanged'
    91:           else:
    92:             print 'Writing new local file',self.local_filename
    93: 
    94:           # note that the local file is written even if it isn't changed
    95:           # to update the time stamp
    96:           file = open(self.local_filename,'w')
    97:           file.writelines(newlines)
    98:           file.close()
    99: 
   100:         except:
   101:           print 'Cannot download',self.remote_filename,
   102:           if hasattr(self,'remote_directory'):
   103:             print 'from directory',self.remote_directory
   104:           else: print 'of',self.host
   105:           try:
   106:             print 'code',errcode,'msg',errmsg
   107:           except:
   108:             pass
   109:       except:
   110:         pass # ignore errors from ftp attempt
   111:     else:
   112:       print 'Skipping http download'
   113: 
   114:     self.file = open(self.local_filename,'r')
   115: 
   116:   def __del__(self):
   117:     self.file.close()
   118: 
   119:   def readline(self):
   120:     line = self.file.readline()
   121:     if len(line)==0: raise eof
   122:     self.lines_read = self.lines_read + 1
   123:     return line
   124: 
   125:   def get_filename(self):
   126:     return self.name
   127: 
End python section to interscript/drivers/sources/http.py[1]