[Tutor] urllib2.urlopen()

eryksun eryksun at gmail.com
Sun Oct 14 13:48:01 CEST 2012


On Sun, Oct 14, 2012 at 5:44 AM, Ray Jones <crawlzone at gmail.com> wrote:
>
> I have often used help() to find my way around imported libraries. I
> didn't realize it would also help with instances. That's good to know.

help(e) shows info for the instance's class, such as methods and data
descriptors (i.e. properties, slots) broken down by inheritance.

Use dir(e) to list all attributes of an instance, or list(vars(e)) if
it has a __dict__.

This is helpful with an instance of HTTPError because the file-like
methods are set as instance data by urllib.addbase. You wouldn't know
about them based on just the help command (but the docs do say it has
a file-like interface).

    >>> list(vars(e))
    ['fp', 'fileno', 'code', 'hdrs', 'read', 'readlines', 'next',
    'headers', '__iter__', 'url', 'msg', 'readline']

For example, e.read() is a method of a socket._fileobject:

    >>> e.read.__self__
    <socket._fileobject object at 0xa107f2c>

This _fileobject is also at e.fp.fp. It wraps the socket interface of
the HTTPResponse, which in turn has the header data in its msg
attribute:

    >>> e.fp.fp
    <socket._fileobject object at 0xa107f2c>

    >>> e.fp.fp._sock
    <httplib.HTTPResponse instance at 0xa0fcf4c>

    >>> e.fp.fp._sock.msg
    <httplib.HTTPMessage instance at 0xa0fcfac>

    >>> e.hdrs
    <httplib.HTTPMessage instance at 0xa0fcfac>


More information about the Tutor mailing list