Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting a `Cookie` in a `CookieJar`

I'm using the new Python Requests library to make http requests. I obtain a cookie from the server as text. How do I turn that into a CookieJar with the cookie in it?

like image 486
Ram Rachum Avatar asked Jul 29 '11 20:07

Ram Rachum


People also ask

Can I put homemade cookies in a cookie jar?

Keep Your Cookies In An Airtight ContainerTo make this kind of cookie jar airtight, just slip your cookies in a zipper-lock bag before you put them in your jar. Once you've selected your cookie container, and you're ready to stash your cookies, try layering them.

Can you put cookies in a glass jar?

The fat in cookies tends to turn rancid, and the flour becomes stale for cookies in an unsealed cookie jar. Because bacteria, humidity, dirt, and dust can reach the cookies, causing the cookies to grow moldy and unpalatable.

How do you add cookies in Python?

#!/usr/bin/python print "Set-Cookie:UserID = XYZ;\r\n" print "Set-Cookie:Password = XYZ123;\r\n" print "Set-Cookie:Expires = Tuesday, 31-Dec-2007 23:12:40 GMT";\r\n" print "Set-Cookie:Domain = www.tutorialspoint.com;\r\n" print "Set-Cookie:Path = /perl;\n" print "Content-type:text/html\r\n\r\n" ...........


1 Answers

I'm confused by this question. The requests library will put the cookies in the jar for you.

import requests import cookielib   URL = '...whatever...' jar = cookielib.CookieJar() r = requests.get(URL, cookies=jar) r = requests.get(URL, cookies=jar) 

The first request to the URL will fill the jar. The second request will send the cookies back to the server. The same goes for the standard library's urllib module cookielib. (doc currently available for 2.x Version)

like image 96
dstanek Avatar answered Oct 01 '22 07:10

dstanek