1

Possible Duplicate:
python: urllib2 how to send cookie with urlopen request

If I have cookies in my browser, how can I have a python script use these saved cookies for a url request?

For a particular site, I'm looking to find a way for my python script to take the identity of me (as i'm logged in on the browser) and then make a request to the server. It does not need to read the cookies from disk, but rather I would like to just do something like this:

cookies = {"username": "USERNAME", "session": "xxxxxxx"...}
urlopen(url, cookies=cookies) # I know that this doesn't work, just showing you what I intend to do

Is there an easy way of doing this?

Community
  • 1
  • 1
C0deH4cker
  • 3,959
  • 1
  • 24
  • 35

2 Answers2

3

Cookielib is your friend here. But you'll need a little more information from your browser than just the cookie name and value:

cookies = {"username": "USERNAME", "session": "xxxxxxx"...}

If you provide these values you should be able to recreate the session in your request:

cookies = [{
    "name": "username",
    "value": "USERNAME",
    "domain": "domain",
    "path": "path",
    "secure": "secure",
}]

Recreate the session with:

import cookielib
import urllib2

cj = cookielib.CookieJar()

for cookie in cookies:
    c = cookielib.Cookie(version=1,
                         name=cookie["name"],
                         value=cookie["value"],
                         port=None,
                         port_specified=False,
                         domain=cookie["domain"],
                         domain_specified=False,
                         domain_initial_dot=False,
                         path=cookie["path"],
                         path_specified=True,
                         secure=cookie["secure"],
                         expires=None,
                         discard=True,
                         comment=None,
                         comment_url=None,
                         rest={'HttpOnly': None},
                         rfc2109=False)
    cj.set_cookie(c)

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.open(urllib2.Request('http://www.example.com')

Untested code.

nathancahill
  • 10,452
  • 9
  • 51
  • 91
1

Firefox and Chrome use SQLite to write their cookies.

You can write an adapter that fetches the cookies from there. I found this example of how to do it and set the cookies in a CookieJar, but I guess you can actually subclass the python CookieJar class to do this on the fly for you and store the values back.

http://www.guyrutenberg.com/2010/11/27/building-cookiejar-out-of-firefoxs-cookies-sqlite/

Not_a_Golfer
  • 47,012
  • 14
  • 126
  • 92
  • I said in the question that i did not want the program to read the cookies from disk (as in the sql database, or anything else), but rather I manually type in the cookies like a dictionary from the output of `javascript:document.write(document.cookie);`. Also, would it be possible for the python script to automatically parse the output of the above js command into a dictionary, so all I'd need to do is input the string? Thanks. – C0deH4cker Jun 01 '12 at 23:30
  • 1
    have a look at the cookielib API, you can add cookies to a request. http://docs.python.org/library/cookielib.html#cookielib.CookieJar.set_cookie – Not_a_Golfer Jun 01 '12 at 23:35