Guy Rutenberg

Keeping track of what I do

Building CookieJar out of Firefox’s cookies.sqlite

with 3 comments

Firefox 3 started to store it’s cookies in a SQLite database instead of the old plain-text cookie.txt. While Python’s cookielib module could read the old cookie.txt file, it doesn’t handle the new format. The following python snippet takes a CookieJar object and the path to Firefox cookies.sqlite (or a copy of it) and fills the CookieJar with the cookies from cookies.sqlite.

import sqlite3
import cookielib
 
def get_cookies(cj, ff_cookies):
    con = sqlite3.connect(ff_cookies)
    cur = con.cursor()
    cur.execute("SELECT host, path, isSecure, expiry, name, value FROM moz_cookies")
    for item in cur.fetchall():
        c = cookielib.Cookie(0, item[4], item[5],
            None, False,
            item[0], item[0].startswith('.'), item[0].startswith('.'),
            item[1], False,
            item[2],
            item[3], item[3]=="",
            None, None, {})
        print c
        cj.set_cookie(c)

It works well for me, except that apperantly Firefox doesn’t save session cookies to the disk at all.

Written by Guy

November 27th, 2010 at 5:19 pm

Posted in Projects

Tagged with ,

3 Responses to 'Building CookieJar out of Firefox’s cookies.sqlite'

Subscribe to comments with RSS or TrackBack to 'Building CookieJar out of Firefox’s cookies.sqlite'.

  1. Thanks, this is exactly what I was looking for. I wanted to download a cookie-protected page and I found a solution with Python + wget (see http://bit.ly/pp25LP). Now with this snippet I can build a full-Python solution.

    Jabba Laci

    11 Sep 11 at 05:48

  2. [...] 1: extracting cookies and storing them in a cookiejar On the blog of Guy Rutenberg I found a post that explains this step. Here is my slightly refactored [...]

  3. Hey man,

    Thanks for the tip. I’m building a cookie dict from the Chrome cookie sql, and this was just the trick I was looking for.

    TankorSmash

    TankorSmash

    29 Jul 12 at 04:00

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>