cURL / Mailing Lists / curl-library / Single Mail

curl-library

pycurl and cookies

From: Robert D. Young <Robert_at_AbilitySys.com>
Date: Sun, 23 May 2004 13:23:26 -0700

All:
 
I've been playing with cookies and pycurl and can't seem to get the
effect that I want. I need to do the following steps:
 
1. Determine the pre-existence of a cookie file (done)
2. Send the first URL request with the pre-existing cookie data (done)
3. Handle incoming cookies (somewhat, see below)
4. Send subsequent URL requests with both pre-existing cookie data and
new cookie data (no, see below)
 
I wrote my own code to locate and parse the existing cookie file without
a problem. But setting pycurl options COOKIEFILE and COOKIEJAR wouldn't
send the data. In fact, the cleanup wrote over the file, so I copy the
existing cookie file to a new file first, using:
            newcookiefile = os.tempnam()
            inp = open(originalcookiefile,'rb')
            outp = open(newcookiefile,'wb')
            outp.write(inp.read())
            inp.close()
            outp.close()
 
Then I read the file myself and set the COOKIE value to the text in the
file:
 
            c.setopt(pycurl.COOKIEFILE, newcookiefile)
            c.setopt(pycurl.COOKIEJAR, newcookiefile)
            c.setopt(pycurl.COOKIE, "key=val; key2=val2; key3=val3")
 
I had tried just the first two lines, but no cookies were sent with the
requests so I added the COOKIE setting and we are go for at least the
first pass. However, when a subsequent request goes out, it has the
following form:
 
            GET <myurl> HTTP/1.0
            Proxy-Authorization: <auth stuff>
>>> 1 Cookie: key=val; key2=val2; key3=val3
            Host: <myhost>
            Pragma: no-cache
            Accept: */*
            Referer: <myreferrer>
>>> 2 Cookie: key4=val4; key5=val5
            User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Win32)
 
Where the values in the third line (>>> 1) are the original values
(somehow retained since I didn't re-set the COOKIE option between
requests) and the values in the eighth line (>>> 2) are from the cookies
received during the session, automagically set by cURL. However, I think
the second line overrides the first at the server, so it never sees the
first set of cookies and my request fails.
 
How can I get a combination of both the pre-existing cookies and the
session cookies to be sent?
 
Thanks in advance,
 
- Robert
 
 
Received on 2004-05-23