[ACCEPTED]-Accept Cookies in Python-cookies
Try this:
import urllib2
import cookielib
jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
print "Currently have %d cookies" % len(jar)
print "Getting page"
response = opener.open("http://google.com")
print response.headers
print "Got page"
print "Currently have %d cookies" % len(jar)
print jar
It should print
Currently have 0 cookies
...
Currently have 2 cookies
(Google always sets 5 a cookie). You don't really need this much 4 unless you want to save your cookies to 3 disk and use them later. You should find 2 that
urllib2.build_opener(HTTPCookieProcessor).open(url)
Takes care of most of what you want.
More 1 info here:
The easiest way is to use requests library.
import requests
url = 'http://www.google.com/doodles/'
r = requests.get(url)
print r.cookies
0
I believe you mean having a Python script 6 that tries to speak HTTP. I suggest you 5 to use a high-level library that handles 4 cookies automatically. pycurl, mechanize, twill 3 - you choose.
For Nikhil Chelliah:
I don't 2 see what's not clear here.
Accepting a cookie happens 1 client-side. The server can set a cookie.
There's the cookielib library. You can also 5 implement your own cookie storage and policies, the 4 cookies are found in the set-cookie header 3 of the response (Set-Cookie: name=value), then 2 you send the back to a server in one or 1 more Cookie headers in the request (Cookie: name=value).
It's unclear whether you want a client-side 7 or a server-side solution.
For client-side, cookielib will 6 work fine. This answer and a few web tutorials offer 5 more in-depth explanations.
If this is a 4 server-side problem, you should be using 3 a framework that takes care of all the boilerplate. I 2 really like how CherryPy and web.py handle them, but the 1 API is pretty simple in any library.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.