forked from cztomczak/cefpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetcookie.py
More file actions
64 lines (52 loc) · 1.8 KB
/
Copy pathsetcookie.py
File metadata and controls
64 lines (52 loc) · 1.8 KB
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
55
56
57
58
59
60
61
62
63
64
"""
Shows how to set a cookie.
"""
from cefpython3 import cefpython as cef
import datetime
class LoadHandler(object):
def OnLoadEnd(self, browser, frame, http_code, **_):
if not frame.IsMain():
return
manager = cef.CookieManager.GetGlobalManager()
cookie = cef.Cookie()
cookie.Set({
"name": "my_cookie",
"value": "my_value",
# Make sure domain is a valid value otherwise it crashes
# app (Issue #459)
"domain": ".google.com",
"path": "/",
"secure": True,
"httpOnly": False,
"creation": datetime.datetime(2018, 8, 22),
"lastAccess": datetime.datetime(2018, 8, 22),
"hasExpires": True,
"expires": datetime.datetime(2028, 12, 31, 23, 59, 59),
})
manager.SetCookie("https://www.google.com/", cookie)
print("Cookie set: my_cookie=my_value")
# Delay so SetCookie (async on IO thread) completes before visiting
cef.PostDelayedTask(cef.TID_UI, 200, visit_cookies)
def visit_cookies():
manager = cef.CookieManager.GetGlobalManager()
manager.VisitUrlCookies(
"https://www.google.com/",
False,
CookieVisitor())
class CookieVisitor(object):
def Visit(self, cookie, count, total, delete_cookie_out):
print("Cookie[%d/%d]: %s=%s (domain=%s)" % (
count + 1, total,
cookie.Get("name"), cookie.Get("value"),
cookie.Get("domain")))
return True # continue visiting
def main():
cef.Initialize()
browser = cef.CreateBrowserSync(
url="https://www.google.com/",
window_title="Set a cookie")
browser.SetClientHandler(LoadHandler())
cef.MessageLoop()
cef.Shutdown()
if __name__ == '__main__':
main()