pwsm / httplib2

Automatically exported from code.google.com/p/httplib2
0 stars 0 forks source link

Cookie enhancement #178

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I have working with httplib2 in order to make some robots, but I found that it 
takes the cookies from an httplib.HTTPResponse object using getheaders() method.

This method uses a dictionary that merges all the 'Set-Cookie' headers, so if 
you want to use the cookielib.CookieJar library you're gonna have problems. (or 
any other cookie manager)

However httplib.HTTPResponse provides the raw data using msg.headers variable. 
The code would be that:

class Response(dict):
    """An object more like email.Message than httplib.HTTPResponse."""

    """Is this response from our local cache"""
    fromcache = False

    """HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1. """
    version = 11

    "Status code returned by server. "
    status = 200

    """Reason phrase returned by server."""
    reason = "Ok"

    previous = None

    def __init__(self, info):
        # info is either an email.Message or 
        # an httplib.HTTPResponse object.
        if isinstance(info, httplib.HTTPResponse):
            #for key, value in info.getheaders():      #doesn't work 
               # self[key.lower()] = value                  # doesn't work
            self["headers"]=[(i.split(":",1)[0],i.split(":",1)[1].strip()) for i in info.msg.headers]         # store headers in self["headers"] instead
            self.status = info.status
            self['status'] = str(self.status)
            self.reason = info.reason

Original issue reported on code.google.com by deepbit on 24 Sep 2011 at 3:51