urlgrabber.byterange
index
/home/groups/urlgrabber/web/contents/urlgrabber/byterange.py

#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public
#   License as published by the Free Software Foundation; either
#   version 2.1 of the License, or (at your option) any later version.
#
#   This library is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public
#   License along with this library; if not, write to the 
#      Free Software Foundation, Inc., 
#      59 Temple Place, Suite 330, 
#      Boston, MA  02111-1307  USA

 
Modules
       
ftplib
mimetools
mimetypes
os
rfc822
socket
stat
sys
urllib
urllib2

 
Classes
       
exceptions.IOError(exceptions.EnvironmentError)
RangeError
RangeableFileObject
urllib.ftpwrapper
ftpwrapper
urllib2.BaseHandler
HTTPRangeHandler
HTTPSRangeHandler
urllib2.FTPHandler(urllib2.BaseHandler)
FTPRangeHandler
urllib2.FileHandler(urllib2.BaseHandler)
FileRangeHandler

 
class FTPRangeHandler(urllib2.FTPHandler)
    
Method resolution order:
FTPRangeHandler
urllib2.FTPHandler
urllib2.BaseHandler

Methods defined here:
connect_ftp(self, user, passwd, host, port, dirs)
ftp_open(self, req)

Methods inherited from urllib2.BaseHandler:
__lt__(self, other)
add_parent(self, parent)
close(self)

Data and other attributes inherited from urllib2.BaseHandler:
handler_order = 500

 
class FileRangeHandler(urllib2.FileHandler)
    FileHandler subclass that adds Range support.
This class handles Range headers exactly like an HTTP
server would.
 
 
Method resolution order:
FileRangeHandler
urllib2.FileHandler
urllib2.BaseHandler

Methods defined here:
open_local_file(self, req)

Methods inherited from urllib2.FileHandler:
file_open(self, req)
# Use local file or FTP depending on form of URL
get_names(self)

Data and other attributes inherited from urllib2.FileHandler:
names = None

Methods inherited from urllib2.BaseHandler:
__lt__(self, other)
add_parent(self, parent)
close(self)

Data and other attributes inherited from urllib2.BaseHandler:
handler_order = 500

 
class HTTPRangeHandler(urllib2.BaseHandler)
    Handler that enables HTTP Range headers.
 
This was extremely simple. The Range header is a HTTP feature to
begin with so all this class does is tell urllib2 that the 
"206 Partial Content" reponse from the HTTP server is what we 
expected.
 
Example:
    import urllib2
    import byterange
    
    range_handler = range.HTTPRangeHandler()
    opener = urllib2.build_opener(range_handler)
    
    # install it
    urllib2.install_opener(opener)
    
    # create Request and set Range header
    req = urllib2.Request('http://www.python.org/')
    req.header['Range'] = 'bytes=30-50'
    f = urllib2.urlopen(req)
 
  Methods defined here:
http_error_206(self, req, fp, code, msg, hdrs)
http_error_416(self, req, fp, code, msg, hdrs)

Methods inherited from urllib2.BaseHandler:
__lt__(self, other)
add_parent(self, parent)
close(self)

Data and other attributes inherited from urllib2.BaseHandler:
handler_order = 500

 
class HTTPSRangeHandler(HTTPRangeHandler)
    Range Header support for HTTPS.
 
 
Method resolution order:
HTTPSRangeHandler
HTTPRangeHandler
urllib2.BaseHandler

Methods defined here:
https_error_206(self, req, fp, code, msg, hdrs)
https_error_416(self, req, fp, code, msg, hdrs)

Methods inherited from HTTPRangeHandler:
http_error_206(self, req, fp, code, msg, hdrs)
http_error_416(self, req, fp, code, msg, hdrs)

Methods inherited from urllib2.BaseHandler:
__lt__(self, other)
add_parent(self, parent)
close(self)

Data and other attributes inherited from urllib2.BaseHandler:
handler_order = 500

 
class RangeError(exceptions.IOError)
    Error raised when an unsatisfiable range is requested.
 
 
Method resolution order:
RangeError
exceptions.IOError
exceptions.EnvironmentError
exceptions.StandardError
exceptions.Exception

Methods inherited from exceptions.EnvironmentError:
__init__(...)
__str__(...)

Methods inherited from exceptions.Exception:
__getitem__(...)

 
class RangeableFileObject
    File object wrapper to enable raw range handling.
This was implemented primarilary for handling range 
specifications for file:// urls. This object effectively makes 
a file object look like it consists only of a range of bytes in 
the stream.
 
Examples:
    # expose 10 bytes, starting at byte position 20, from 
    # /etc/aliases.
    >>> fo = RangeableFileObject(file('/etc/passwd', 'r'), (20,30))
    # seek seeks within the range (to position 23 in this case)
    >>> fo.seek(3)
    # tell tells where your at _within the range_ (position 3 in
    # this case)
    >>> fo.tell()
    # read EOFs if an attempt is made to read past the last
    # byte in the range. the following will return only 7 bytes.
    >>> fo.read(30)
 
  Methods defined here:
__getattr__(self, name)
This effectively allows us to wrap at the instance level.
Any attribute not found in _this_ object will be searched for
in self.fo.  This includes methods.
__init__(self, fo, rangetup)
Create a RangeableFileObject.
fo       -- a file like object. only the read() method need be 
            supported but supporting an optimized seek() is 
            preferable.
rangetup -- a (firstbyte,lastbyte) tuple specifying the range
            to work over.
The file object provided is assumed to be at byte offset 0.
read(self, size=-1)
Read within the range.
This method will limit the size read based on the range.
readline(self, size=-1)
Read lines within the range.
This method will limit the size read based on the range.
seek(self, offset, whence=0)
Seek within the byte range.
Positioning is identical to that described under tell().
tell(self)
Return the position within the range.
This is different from fo.seek in that position 0 is the 
first byte position of the range tuple. For example, if
this object was created with a range tuple of (500,899),
tell() will return 0 when at byte position 500 of the file.

 
class ftpwrapper(urllib.ftpwrapper)
     Methods defined here:
retrfile(self, file, type, rest=None)
# range support note:
# this ftpwrapper code is copied directly from
# urllib. The only enhancement is to add the rest
# argument and pass it on to ftp.ntransfercmd

Methods inherited from urllib.ftpwrapper:
__init__(self, user, passwd, host, port, dirs)
close(self)
endtransfer(self)
init(self)

 
Functions
       
StringIO(...)
StringIO([s]) -- Return a StringIO-like stream for reading or writing
range_header_to_tuple(range_header)
Get a (firstbyte,lastbyte) tuple from a Range header value.
 
Range headers have the form "bytes=<firstbyte>-<lastbyte>". This
function pulls the firstbyte and lastbyte values and returns
a (firstbyte,lastbyte) tuple. If lastbyte is not specified in
the header value, it is returned as an empty string in the
tuple.
 
Return None if range_header is None
Return () if range_header does not conform to the range spec 
pattern.
range_tuple_normalize(range_tup)
Normalize a (first_byte,last_byte) range tuple.
Return a tuple whose first element is guaranteed to be an int
and whose second element will be '' (meaning: the last byte) or 
an int. Finally, return None if the normalized tuple == (0,'')
as that is equivelant to retrieving the entire file.
range_tuple_to_header(range_tup)
Convert a range tuple to a Range header value.
Return a string of the form "bytes=<firstbyte>-<lastbyte>" or None
if no range is needed.

 
Data
        DEBUG = None