It was supposed to be done by a Browsercookie library, but at first glance it had a lot of problems:
- You cannot specify the Firefox profile you want to use (later found you can specify a database file).
- 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:
- Search for candidate cookies by naked domain name
- Filter cookies based on attributes such as domain name, path, and secure
- 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