开发者

how do I get pycurl to send cookies but not save them to file?

开发者 https://www.devze.com 2023-01-27 17:47 出处:网络
I need pycurl to save cookies from a poste开发者_JS百科d form and then use those cookies in the next URL which is in the same domain. It doesn\'t seem to do this automatically.

I need pycurl to save cookies from a poste开发者_JS百科d form and then use those cookies in the next URL which is in the same domain. It doesn't seem to do this automatically.

Ive read the COOKIEFILE and COOKIEJAR options but I dont want to save these cookies to file. So is there some other way of enabling cookies with curl/pycurl without saving them to file?


Well the libcurl docs states that:

CURLOPT_COOKIEFILE

Given an empty or non-existing file or by passing the empty string (""), this option will enable cookies for this curl handle, making it understand and parse received cookies and then use matching cookies in future requests.

Just tried it myself and it works brilliantly, just use the same curl object.

Example

import pycurl
curl = pycurl.Curl()

# Turn on cookies
curl.setopt(pycurl.COOKIEFILE, "")

# Login
curl.setopt(pycurl.URL, "http://www.example.com/login.php")
curl.setopt(pycurl.POST, 1)
curl.setopt(pycurl.HTTPPOST, [('user', 'myuser'), ('pass', 'mypass'), ('submit', 'login')])
curl.perform()

# Now let's get us some members only page
curl.setopt(pycurl.POST, 0) 
curl.setopt(pycurl.URL, "http://www.example.com/members_only.php")
curl.perform()

I skipped the whole StringIO response getting so we could stay ontopic.


According to the PycURL site, the real documentation is the C API. The C API CURLOPT_COOKIE and CURLOPT_COOKIELIST that on a cursory glace look like what you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号