Simplify getting and storing cookies from the browser to use in Python
Project description
CookieCache
Simplify getting and using cookies from the browser to use in Python.
NOTE: All the logic to interface with the browsers is done by the Browser Cookie 3 library.
This code wraps that BrowserCookie3 in a CLI and library that caches the selected cookies as JSON to disk, so they can be transferred to other machines, or be used again without having to interactivly re-enter MacOS user credentials.
Install
pip install cookiecache
Usage
The idea is you run cookiecache once (either as a cli tool or as a library), and then cache the cookies to JSON on disk. Then the next time you run the same thing, cookiecache will look in the JSON file first, and only get fresh cookies from the browser if they have expired.
CLI
Examples:
# Get github session cookies and save to disk
cookiecache --domain "github.com" --cookie "_gh_sess" --filename "cookies.json"
# Get all github cookies in Netscape/Mozilla format to use with curl, et.c:
cookiecache --domain "github.com" --filename "cookies.txt" --curl
# All options:
cookiecache --help
Library
To use cookiecache as a library call load()
with arguments similar to
the cli:
import cookiecache
# Get github session cookies and save to disk
# After the first run this will load the cookies
# from disk first, and only get them from the brower
# if they have expired
cookies = cookiecache.load(
domain="github.com",
cookie="_gh_sess",
filename="cookies.json",
)
# Load all cookies from JSON file from another machine, ie.
# Don't attempt to refresh or get cookies from this machine
cookies = cookiecache.load(
filename="cookies.json",
check_expiry=False
)
# Get cookies only from Chrome, and don't read or save to disk
cookies = cookiecache.load(
domain="github.com",
browser="chrome",
)
# Convert cookies from cookiecache to a flat key-value
# pair to use with Requests
cookies = cookiecache.load(domain="github.com")
cookies_flat = cookiecache.flatten_cookies(cookies)
reqeusts.get("http://github.com", cookies=cookies_flat)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for cookiecache-1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c507038962d6f0cf1e9d4b0b2d72875c5c5f3e5001b1bc647698b9cb0209465 |
|
MD5 | 1d57ed87c28b07c03dc32f0ddec28186 |
|
BLAKE2b-256 | 8ef20ec41a7afeebc2725fe727aba2b56ab0eec0e21ab859f058cc9a92c76627 |