try-catch-finally try-catch-finally - 9 months ago 43
Python Question

PyCurl 7.19.0: get cookies from response using getinfo(pycurl.COOKIELIST)

I'd like to get the Cookies that have been carried with the response (

Set-Cookie: name=value; ...

When passing the info constant and the reference of an empty list:

set_cookies = []
c.getinfo(c.COOKIELIST, set_cookies)

I get the following error:

TypeError: c.getinfo() takes exactly 1 argument (2 given)

That was straightforward.

I also tried this signature:

set_cookies = c.getinfo(c.COOKIELIST)

I get this error:

ValueError: invalid argument to getinfo

This is a bit vague, however.

Getting the HTTP status code using
works fine.

I get a
200 OK
headers, of course (debugging with
c.setopt(pycurl.VERBOSE, 1)

Of course I've read

With very few exceptions, PycURL constants are derived from libcurl constants by removing the CURLINFO_ prefix.

Maybe this is the exception?

In my opinion the documentation is a quite poor and does not contain any info about getting cookies sent with


Answer Source

There's no method to get response cookies after calling perform(). Headers sent by the server can be captured using a callback function passed to the PyCurl instance while configuring with setopt():


   Callback for writing received headers. See CURLOPT_HEADERFUNCTION 

Example code excerpt:

    set_cookies = []

    # closure to capture Set-Cookie
    def _write_header(header):
        match = re.match("^Set-Cookie: (.*)$", header)

        if match:

    # use closure to collect cookies sent from the server
    c.setopt(pycurl.HEADERFUNCTION, _write_header)


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download