Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ubuntu Chrome: How to read a cookie from a python script

I am creating a little application that has two parts: One of them is displayed inside a Chrome browser and the other is a local application programmed in Python.

In Chrome, the user has a <select> to choose his/her preferred language. That information is stored by Chrome in a cookie.

I would like to know if it's possible to retrieve that language preference (meaning, reading the cookie) so when I run the local application, it will be displayed on the same language the user already selected with Chrome.

I can not change the system's locale, though (which is what would probably make the most sense). That has to remain in English, but if the user selects Chinese as preferred language through Chrome, and then launches the local application, I would like that application to be able to start in Chinese.

I've been looking at the command line switches for Chrome, but I haven't seen anything too helpful. At most, the --enable-file-cookies option, and then try to open and parse the cookie file... somehow, but all the information I've been able to find is pretty vague.

Thank you in advance!

Update. Further searching (1, 2) seems to indicate that Chrome stores the cookies using SQL lite. I am looking into this. Maybe there's hope with that...

like image 363
BorrajaX Avatar asked Apr 12 '12 15:04

BorrajaX


People also ask

How do I view browser cookies in Python?

How do I get cookie data in python? Use the make_response() function to get the response object from the return value of the view function. After that, the cookie is stored using the set_cookie() function of the response object. It is easy to read back cookies.

How do I extract cookies?

Copy all cookies used in the current tab to the clipboard. This extension can be called by the shortcut key "Ctrl+Shift+K". It works as like as the export feature of the EditThisCookie (http://editthiscookie.com/). The copied cookie objects can be used by puppeteer (https://github.com/puppeteer/puppeteer).


1 Answers

Yep, as I mentioned in the comments to my question, sqlite3 sounded promising... The day I learn to read, I'll conquer the world!!

Anyway, just in case is helpful for someone else:

#!/usr/bin/env python
import os
import sqlite3
import pwd

_cookieName = "preferredLanguage"

def getPreferredLanguageFromCookieDB():
    retval="en-US"
    cookieDBFilename = os.path.join(pwd.getpwuid(1000).pw_dir, ".config/google-chrome/Default/Cookies")
    if os.path.isfile(cookieDBFilename):
        connection = sqlite3.connect(cookieDBFilename)
        querier = connection.cursor()
        numCookiesMatching = int(querier.execute('SELECT COUNT(*) FROM cookies WHERE (host_key="127.0.0.1" or host_key="localhost") and name="%s"' % (_cookieName)).fetchone()[0])
        if numCookiesMatching == 1:
            retval = querier.execute('SELECT value FROM cookies WHERE (`cookies`.`host_key`="127.0.0.1" or `cookies`.`host_key`="localhost") and `cookies`.`name` = "%s"' % (_cookieName)).fetchone()[0]
        elif numCookiesMatching == 0:
            print("::getPreferredLanguageFromCookieDB > No cookie for '%s' found. Assuming wizard hasn't run yet, which is weird, but not critical" % (_cookieName))
            retval="en-US"
        else:
            raise KeyError("Found %s cookies matching %s in file %s. This shouldn't have happened" % (numCookiesMatching, _cookieName, cookieDBFilename))
            retval=None
    else:
        print("::getPreferredLanguageFromCookieDB > Cookie 'db' (actually, file) %s doesn't exist" % (cookieDBFilename))
        retval="en-US"

    return retval


if __name__ == "__main__":
    print "Prefered language: %s" % getPreferredLanguageFromCookieDB()

This little snippet will connect to the Cookies "database" (is actually just file but, anyway...) and read the value of the "preferredLanguage" cookie issued by either localhost or 127.0.0.1. It will crash if there's more than one "preferredLanguage" cookie issued by localhost.

like image 179
BorrajaX Avatar answered Oct 20 '22 00:10

BorrajaX