import ssl
import http.cookiejar
import urllib.request
from urllib.request import ProxyHandler
from urllib.request import HTTPCookieProcessor
# SSL証明書のエラーを無視する設定
ssl._create_default_https_context = ssl._create_unverified_context
target = '127.0.0.1'
port = '8080'
properties = [
'domain',
'version',
'name',
'value',
'port',
'path',
'secure',
'expires',
'discard',
'comment',
'comment_url',
'rfc2109',
'port_specified',
'domain_specified',
'domain_specified',
'domain_initial_dot',
]
functions = [
('has_nonstandard_attr',{'name':'secure'}),
('get_nonstandard_attr',{'name':'secure'}),
('set_nonstandard_attr',{'name':'test','value':123}),
('is_expired',{})
]
#proxy
proxy_handler = ProxyHandler({'http':target + ':' + port,'https':target + ':' + port})
#Cookieの読み込み、書き込みをするためにはLWPCookieJarを使用
cj = http.cookiejar.LWPCookieJar()
#cj = http.cookiejar.FileCookieJar()
try:
cj.load('cookie.txt')
except:
pass
opener = urllib.request.build_opener(HTTPCookieProcessor(cj),proxy_handler)
r = opener.open("https://google.co.jp/")
r = opener.open("https://twitter.com/")
for cookie in cj:
for property in properties:
print('{0:20} : {1}'.format(property,cookie.__dict__[property]))
for function in functions:
tmp_function,param = function
print('{0:20} : {1}'.format(tmp_function,getattr(cookie,tmp_function)(**param)))
cj.save(filename='cookie.txt')