How to update the cookie in a request header?
I'm a newbie to python / web development and am working on a script logging in the google account with urllib2 and cookielib. Currently I get stuck by the 'Set-Cookie' staff.
In the very beginning, I'm using httplib to send request. When I post a login request, the response headers contain a dozen of 'Set-Cookie' entries like the following -
Set-Cookie: GAPS=1:HrRi-Z_IxwESn6DOHyY2qYkBFKK8_Q:csBfgQOtNDoeJhew;Path=/;Expires=Wed, 18-Sep-2013 07:27:34 GMT;Secure;HttpOnly
Set-Cookie: SID=DQAAAOcAAACx9rjyWTFqBgH2p开发者_StackOverflow中文版8rHC7LS4C2yCfDcC8kW004wqkxteUf_EIJdzbXoz7Dy0w-60q0EFG57XTylnxmBBXhFR1YTpMrUyfYCBsZdPFKdyq471p6mQKC3nB-_XtuY-lol-WN3e6x8dz7mb20K6kPjefE3G9Y-
Set-Cookie: HSID=ALG231HZqMLBeBtwS;Domain=.google.com;Path=/;HttpOnly
Set-Cookie: SSID=Abl0p2GUu-XzSTN4m;Domain=.google.com;Path=/;Secure;HttpOnly
Set-Cookie: APISID=WVxc29cYqLxHKOyO/A0mu1KzvIbEwFM2WT;Domain=.google.com;Path=/
Set-Cookie: SAPISID=M4wLHhKLE_WvV-Cw/ALvXssXKb1oGTKxMk;Domain=.google.com;Path=/;Secure
After reading quite a few, I aware that these should be included in the next http request to let google know that the client cookie support is enabled. Thus I turned to urllib2 and cookielib. But the problem is that I don't find any method to update the 'Set-Cookie's in my requests. Do you happen to if there's a way to update the cookies according to the server responses, which is easier than handle the raw strings from scratch?
Any help would be greatly appreciated!
Thanks, Kai
Here you can find an example on how to do this. The idea is to have a cookie jar storing all the collected cookies. The jar gets used by urllib2's opener. I hope this helps.
精彩评论