forked from cztomczak/cefpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetwork_cookies.py
More file actions
51 lines (42 loc) · 1.5 KB
/
network_cookies.py
File metadata and controls
51 lines (42 loc) · 1.5 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
"""
Implement RequestHandler.CanGetCookies and CanSetCookie
to block or allow cookies over network requests.
"""
from cefpython3 import cefpython as cef
def main():
cef.Initialize()
browser = cef.CreateBrowserSync(
url="http://www.html-kit.com/tools/cookietester/",
window_title="Network cookies")
browser.SetClientHandler(RequestHandler())
cef.MessageLoop()
del browser
cef.Shutdown()
class RequestHandler(object):
def __init__(self):
self.getcount = 0
self.setcount = 0
def CanGetCookies(self, frame, request, **_):
# There are multiple iframes on that website, let's log
# cookies only for the main frame.
if frame.IsMain():
self.getcount += 1
print("-- CanGetCookies #"+str(self.getcount))
print("url="+request.GetUrl()[0:80])
print("")
# Return True to allow reading cookies or False to block
return True
def CanSetCookie(self, frame, request, cookie, **_):
# There are multiple iframes on that website, let's log
# cookies only for the main frame.
if frame.IsMain():
self.setcount += 1
print("-- CanSetCookie @"+str(self.setcount))
print("url="+request.GetUrl()[0:80])
print("Name="+cookie.GetName())
print("Value="+cookie.GetValue())
print("")
# Return True to allow setting cookie or False to block
return True
if __name__ == '__main__':
main()