save and load cookie

This commit is contained in:
simon987 2019-12-29 13:25:43 -05:00
parent 3357da764c
commit 37257f11eb
2 changed files with 27 additions and 1 deletions

View File

@ -1,6 +1,10 @@
from http.cookiejar import Cookie from http.cookiejar import Cookie
import re
from dateutil.parser import parse from dateutil.parser import parse
import pickle
from requests.cookies import RequestsCookieJar
def cookie_from_string(text: str, domain: str) -> Cookie: def cookie_from_string(text: str, domain: str) -> Cookie:
@ -33,3 +37,25 @@ def cookie_from_string(text: str, domain: str) -> Cookie:
rest=None, rest=None,
rfc2109=False rfc2109=False
) )
def save_cookiejar(cj, filename):
with open(filename, "wb") as f:
f.truncate()
pickle.dump(cj._cookies, f)
def load_cookiejar(filename):
with open(filename, "rb") as f:
cookies = pickle.load(f)
cj = RequestsCookieJar()
cj._cookies = cookies
return cj
def cookiejar_filter(cj, pattern):
filtered_cj = RequestsCookieJar()
for c in cj:
if re.match(pattern, c.domain):
filtered_cj.set_cookie(c)
return filtered_cj

View File

@ -2,7 +2,7 @@ from setuptools import setup
setup( setup(
name="hexlib", name="hexlib",
version="1.4", version="1.5",
description="Misc utility methods", description="Misc utility methods",
author="simon987", author="simon987",
author_email="me@simon987.net", author_email="me@simon987.net",