-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchrome_cookie.py
54 lines (46 loc) · 1.64 KB
/
chrome_cookie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import requests
import pytz
import calendar
from datetime import datetime
from http.cookiejar import CookieJar, Cookie
def load_cookie(loc):
cj = CookieJar()
with open(loc, 'r') as f:
for line in f.readlines():
l = line.strip('\n').split('\t')
while len(l) < 9:
l.append(None)
try:
expires = (pytz.utc.localize(
datetime.strptime(l[4], '%Y-%m-%dT%H:%M:%S.%fZ')))
expires = calendar.timegm(expires.utctimetuple())
except ValueError:
expires = None
ck = Cookie(name=l[0],
value=l[1],
domain=l[2],
path=l[3],
secure='✓' == l[7],
rest={'HttpOnly': '✓' == l[6]},
version=0,
port=None,
port_specified=False,
domain_specified=False,
domain_initial_dot=False,
path_specified=True,
expires=expires,
discard=True,
comment=None, comment_url=None, rfc2109=False)
cj.set_cookie(ck)
return cj
def create_session(cookie_jar):
req = requests.Session()
req.cookies = cookie_jar
return req
if __name__ == '__main__':
req_sess = create_session(load_cookie('test.cookie'))
r = req_sess.get('https://mail.google.com/mail/u/0/#inbox')
print(r.status_code)
with open('out.html', 'w', encoding='utf8') as f:
f.write(r.text)
print('Done!')