Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should consider using the <a href="http://docs.python-requests.org/en/latest/" rel="nofollow noreferrer"><code>Requests</code></a> library instead at the earliest chance you have to refactor your code. In the mean time;</p> <p>HACK ALERT! :)</p> <p>I'd go other suggested way, but I've done a hack (done for different reasons though), which does create an interface between <a href="https://docs.python.org/2/library/httplib.html" rel="nofollow noreferrer">httplib</a> and <a href="https://docs.python.org/2/library/cookielib.html" rel="nofollow noreferrer">cookielib</a>.</p> <p>What I did was creating a fake <code>HTTPRequest</code> with minimal required set of methods, so that <a href="https://docs.python.org/2/library/cookielib.html#cookielib.CookieJar" rel="nofollow noreferrer"><code>CookieJar</code></a> would recognize it and process cookies as needed. I've used that fake request object, setting all the data needed for cookielib.</p> <p>Here is the code of the class:</p> <pre><code>class HTTPRequest( object ): """ Data container for HTTP request (used for cookie processing). """ def __init__( self, host, url, headers={}, secure=False ): self._host = host self._url = url self._secure = secure self._headers = {} for key, value in headers.items(): self.add_header(key, value) def has_header( self, name ): return name in self._headers def add_header( self, key, val ): self._headers[key.capitalize()] = val def add_unredirected_header(self, key, val): self._headers[key.capitalize()] = val def is_unverifiable( self ): return True def get_type( self ): return 'https' if self._secure else 'http' def get_full_url( self ): port_str = "" port = str(self._host[1]) if self._secure: if port != 443: port_str = ":"+port else: if port != 80: port_str = ":"+port return self.get_type() + '://' + self._host[0] + port_str + self._url def get_header( self, header_name, default=None ): return self._headers.get( header_name, default ) def get_host( self ): return self._host[0] get_origin_req_host = get_host def get_headers( self ): return self._headers </code></pre> <p>Please note, the class has support for HTTPS protocol only (all I needed at the moment).</p> <p>The code, which used this class was (please note another hack to make response compatible with cookielib):</p> <pre><code>cookies = CookieJar() headers = { # headers that you wish to set } # construct fake request fake_request = HTTPRequest( host, request_url, headers ) # add cookies to fake request cookies.add_cookie_header(fake_request) # issue an httplib.HTTPConnection based request using cookies and headers from the fake request http_connection.request(type, request_url, body, fake_request.get_headers()) response = http_connection.getresponse() if response.status == httplib.OK: # HACK: pretend we're urllib2 response response.info = lambda : response.msg # read and store cookies from response cookies.extract_cookies(response, fake_request) # process response... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload