How to get cookies from web-browser with Python?
Asked Answered
P

2

27

Context:
I am working on a backend access to an OpenID consumer (StackExchange in fact). If I am to provide all possible OpenID providers as an option to the user, then I'd have to simulate browser interaction to authenticate to each of these providers before I could submit the Open ID URL. However, I think I could cut this short by accessing the existing cookies of the user's web-browser, and requesting authentication to the consumer directly with the URL.

Problem:
How to access the user's web-browser's cookies? I've seen very little information on how to do it with Python. This previous question partly answers the problem regarding Firefox, pointing especially to the code sample her below. However, I would need to access cookies from the most common web browsers used on Linux, not just Firefox.

#! /usr/bin/env python
# Protocol implementation for handling gsocmentors.com transactions
# Author: Noah Fontes nfontes AT cynigram DOT com
# License: MIT

def sqlite2cookie(filename):
    from cStringIO import StringIO
    from pysqlite2 import dbapi2 as sqlite

    con = sqlite.connect(filename)

    cur = con.cursor()
    cur.execute("select host, path, isSecure, expiry, name, value from moz_cookies")

    ftstr = ["FALSE","TRUE"]

    s = StringIO()
    s.write("""\
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file!  Do not edit.
""")
    for item in cur.fetchall():
        s.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (
            item[0], ftstr[item[0].startswith('.')], item[1],
            ftstr[item[2]], item[3], item[4], item[5]))

    s.seek(0)

    cookie_jar = cookielib.MozillaCookieJar()
    cookie_jar._really_load(s, '', True, True)
    return cookie_jar

Question: Does Python provide a module that can facilitate cookie extraction from web-browsers? Otherwise, how should I adapt the above code to draw cookies from other browsers, like Chromium etc.?

PS: Or am I looking at the initial problem (i.e. authenticate to the OpenID provider) the wrong way? (I feel I am just replacing a problem by another.)

Paquin answered 11/1, 2012 at 0:8 Comment(6)
You might want to look at #4634908Broads
or https://mcmap.net/q/534726/-retrieving-all-cookies-in-pythonEasterly
@monkut: actually that one is only relevant to keep cookies throughout sessions, which I already know how to do. What I am trying to do is to get cookies from the browser's itself.Paquin
you might want to look at the Cookie module for retrieving cookies. thiscookie = Cookie.SimpleCookie() if u'HTTP_COOKIE' in os.environ.keys(): thiscookie.load(os.environ[u'HTTP_COOKIE'])Easterly
@monkut: the interesting part here is the HTTP_COOKIE in os.environ.keys(), do you have more info somewhere about that? Thanks.Paquin
I imagine this is populated by wsgi or cgi for when incoming requests call your script.Easterly
B
36

I created a module to do exactly that, available here: https://bitbucket.org/richardpenman/browsercookie/

Example usage:

import requests
import browsercookie
cj = browsercookie.chrome()
r = requests.get('http://stackoverflow.com', cookies=cj)

python3 fork: https://github.com/borisbabic/browser_cookie3

Bridal answered 14/4, 2015 at 13:8 Comment(4)
Link is updated. Also available using pip install browsercookie.Possessory
Note to others coming across this post now: The module does not support current Chrome versions.Olivette
The python 2 version (bitbucket.org/richardpenman/browsercookie) works fine for me in the latest Chrome (57)Jackdaw
Recent versions of chrome changed the way to encrypt cookies. Hopefully someone will figure it out and send a patch.Bridal
A
0

Besides browser-cookie3, you can try https://github.com/n8henrie/pycookiecheat -- it worked for me with Chrome on Ubuntu 20.04 as of April 2021.

Aground answered 13/4, 2021 at 20:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.