Package tlib :: Package base :: Module HttpFileRequestor
[hide private]
[frames] | no frames]

Source Code for Module tlib.base.HttpFileRequestor

1 -class HttpFileRequestor(object):
2 """ 3 Helper class for retrieving http files from url 4 """ 5 6 _url = None 7 _size = None 8
9 - def __init__(self, url=None):
10 """ 11 Constructor for class 12 """ 13 self._url = url 14 self._size = self.get_file_size()
15
16 - def get_file_size(self):
17 """ 18 Get the size of file from the url 19 """ 20 with urllib2.urlopen(self._url) as fd: 21 info = fd.info() 22 return int(info.getheader('Content-Length'))
23 24
25 - def tail(self, url=None, length=0):
26 """ 27 Tail the file from the url 28 """ 29 if url: 30 self._url = url 31 self._size = self.get_file_size() 32 #we assume that each line may contains MAX_CHARS_IN_ONE_LINE characters at max 33 start = max(0, self._size - (MAX_CHARS_IN_ONE_LINE * length)) 34 #send the request asking for the last n bytes of the file content from the url 35 myHeaders = {'Range': 'bytes=%i-' % start} 36 req = urllib2.Request(url=self._url, headers=myHeaders) 37 partialFile = urllib2.urlopen(req) 38 39 lines = partialFile.read().strip().split('\n') 40 41 return lines[-length:] if len(lines) > length else lines[1:]
42