Use Python to read Firefox cookies

Source: Internet
Author: User

It was supposed to be done by a Browsercookie library, but at first glance it had a lot of problems:

    1. You cannot specify the Firefox profile you want to use (later found you can specify a database file).
    2. A line of information is printed to standard output when the Sessionstore.js file is not found. For cron scripts, this behavior is very, very annoying.

When I tried to solve these problems, I found an additional problem: it always reads all the cookies. Plus unnecessarily import keyring, Crypto and other libraries, let me want to give up.

So I consider myself to achieve a cookiejar. But found it has the following problems:

    • public interface and implementation details are not clearly separated
    • There is no abstraction for storing and reading cookies, but there is a dictionary

This is very unpleasant to expand, and I don't know how long it will work.

Cookiejar is a very complicated thing, I might as well implement a standalone function that gets a matching cookie and then pass it to the HTTP client library through various poses.

Firefox cookie database file "cookies.sqlite" is a "moz_cookies" table, its structure is very simple. But, how do you make a cookie match? Since the decision to abandon the Python Cookiejar, then do not look at it, directly look at the source of Firefox good.

So go to the DXR to search the source of Firefox. It doesn't take much effort to find the relevant part, and then follow the code to know how to match it:

    1. Search for candidate cookies by naked domain name
    2. Filter cookies based on attributes such as domain name, path, and secure
    3. That's it, there's no third step.

Naked Domain name use tldextract library to do, other attributes matching algorithm directly see the code of Firefox. It's not familiar with C + + code, but it's very well written and easy to understand.

Write the part you need in Python and get a new module--firefoxcookies. In one way, it is convenient to return a dictionary of cookies. For example, in my requestsutils. In the requestsbase, you can do this:

123456789101112 class FireRequests(RequestsBase):  def initialize(self):    self._fc = FirefoxCookies(os.path.expanduser(      ‘~/.mozilla/firefox/nightly/cookies.sqlite‘))  def request(self, url, method=None, *args, **kwargs):    if self.baseurl:      url = urljoin(self.baseurl, url)    cookies = self._fc.get_cookies(url)    return super().request(url, method=method, cookies=cookies)

That's all I need to meet. There are other needs in the future, and then slowly expand.

Use Python to read Firefox cookies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.