本文整理汇总了Python中http.cookiejar.CookieJar方法的典型用法代码示例。如果您正苦于以下问题:Python cookiejar.CookieJar方法的具体用法?Python cookiejar.CookieJar怎么用?Python cookiejar.CookieJar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类http.cookiejar
的用法示例。
在下文中一共展示了cookiejar.CookieJar方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import CookieJar [as 别名]
def __init__(
self,
host,
port=8069,
timeout=120,
version=None,
deserialize=True,
opener=None,
):
super(ConnectorJSONRPC, self).__init__(host, port, timeout, version)
self.deserialize = deserialize
# One URL opener (with cookies handling) shared between
# JSON and HTTP requests
if opener is None:
cookie_jar = CookieJar()
opener = build_opener(HTTPCookieProcessor(cookie_jar))
self._opener = opener
self._proxy_json, self._proxy_http = self._get_proxies()
示例2: __init__
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import CookieJar [as 别名]
def __init__(self, allowcookies = False, cookiejar = None):
'''
:param allowcookies: Accept and store cookies, automatically use them on further requests
:param cookiejar: Provide a customized cookiejar instead of the default CookieJar()
'''
self._connmap = {}
self._requesting = set()
self._hostwaiting = set()
self._pathwaiting = set()
self._protocol = Http(False)
self.allowcookies = allowcookies
if cookiejar is None:
self.cookiejar = CookieJar()
else:
self.cookiejar = cookiejar
self._tasks = []
示例3: __init__
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import CookieJar [as 别名]
def __init__(self, **kwargs):
"""
@param kwargs: Keyword arguments.
- B{proxy} - An http proxy to be specified on requests.
The proxy is defined as {protocol:proxy,}
- type: I{dict}
- default: {}
- B{timeout} - Set the url open timeout (seconds).
- type: I{float}
- default: 90
"""
Transport.__init__(self)
Unskin(self.options).update(kwargs)
self.cookiejar = CookieJar()
self.proxy = {}
self.urlopener = None
示例4: load_html
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import CookieJar [as 别名]
def load_html(url, with_cookies=False, headers={}):
"""Attempts to load an HTML page, returning a BeautifulSoup instance. Raises
any networking or parsing exceptions"""
if with_cookies:
cj = CookieJar()
opener = urlopen.build_opener(urlopen.HTTPCookieProcessor(cj))
else:
opener = urlopen.build_opener()
request = urlopen.Request(url, headers=headers)
response = opener.open(request)
html = response.read().decode('utf-8', errors='replace')
soup = BeautifulSoup(html, 'html.parser')
return soup
示例5: download
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import CookieJar [as 别名]
def download(self, url, retry_count=3, headers=None, proxy=None, data=None):
if url is None:
return None
try:
req = request.Request(url, headers=headers, data=data)
cookie = cookiejar.CookieJar()
cookie_process = request.HTTPCookieProcessor(cookie)
opener = request.build_opener()
if proxy:
proxies = {urlparse(url).scheme: proxy}
opener.add_handler(request.ProxyHandler(proxies))
content = opener.open(req).read()
except error.URLError as e:
print('HtmlDownLoader download error:', e.reason)
content = None
if retry_count > 0:
if hasattr(e, 'code') and 500 <= e.code < 600:
#说明是 HTTPError 错误且 HTTP CODE 为 5XX 范围说明是服务器错误,可以尝试再次下载
return self.download(url, retry_count-1, headers, proxy, data)
return content
示例6: _getCookies
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import CookieJar [as 别名]
def _getCookies(headers):
cj = CookieJar()
cookies = headers.split(',')
for cookie in cookies:
attrs = cookie.split(';')
cookieNameValue = name = attrs[0].strip().split('=')
pathNameValue = name = attrs[1].strip().split('=')
ck = Cookie(version=1, name=cookieNameValue[0],
value=cookieNameValue[1],
port=443,
port_specified=False,
domain=swaDomain,
domain_specified=True,
domain_initial_dot=False,
path=pathNameValue[1],
path_specified=True,
secure=True,
expires=None,
discard=True,
comment=None,
comment_url=None,
rest={'HttpOnly': None},
rfc2109=False)
cj.set_cookie(ck)
return cj
示例7: set_cookies
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import CookieJar [as 别名]
def set_cookies(firefox_cookies_path: Optional[str]):
"""
显式设置Cookies
:param firefox_cookies_path:
:return:
"""
global __COOKIES_PATH
__COOKIES_PATH = firefox_cookies_path
if firefox_cookies_path is None:
warn("Haven't set a valid cookies path, use default.")
get_request_session().cookies = CookieJar()
else:
try:
get_request_session().cookies = _init_cookies(CookieJar(), firefox_cookies_path)
except:
warn("Error while loading firefox cookies from: {}, please check it.".format(__COOKIES_PATH))
# If we have default cookies path, set here
示例8: __request__
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import CookieJar [as 别名]
def __request__(self):
Cookiejar = CookieJar()
opener = build_opener(HTTPCookieProcessor(Cookiejar))
_header = dict(self.headers.items())
if self.cookie:
_header.update({'Cookie': self.cookie})
req = Request(self.url, headers=_header, origin_req_host=self.host)
res = None
try:
res = opener.open(req)
# break
except HTTPError as e:
# if e.code >= 400 and e.code < 500:
return e, None
except (socket.timeout, URLError) as e:
return e, None
except Exception as e:
traceback.print_exc()
return e, None
return res, Cookiejar._cookies
示例9: cookie_friendly_download
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import CookieJar [as 别名]
def cookie_friendly_download(referer_url, file_url, store_dir='.', timeout=1000):
from http.cookiejar import CookieJar
from urllib import request
cj = CookieJar()
cp = request.HTTPCookieProcessor(cj)
opener = request.build_opener(cp)
with opener.open(referer_url) as fin:
fin.headers.items()
import os
from os import path
with opener.open(file_url, timeout=timeout) as fin:
file_bin = fin.read()
filename = fin.headers['Content-Disposition']
filename = filename.split(';')[-1].split('=')[1]
os.makedirs(store_dir, exist_ok=True)
with open(path.join(store_dir, filename), mode='wb') as fout:
fout.write(file_bin)
return path.join(store_dir, filename)
示例10: __init__
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import CookieJar [as 别名]
def __init__(self, app: "Quart", use_cookies: bool = True) -> None:
self.cookie_jar: Optional[CookieJar]
if use_cookies:
self.cookie_jar = CookieJar()
else:
self.cookie_jar = None
self.app = app
self.push_promises: List[Tuple[str, Headers]] = []
self.preserve_context = False
示例11: get_response
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import CookieJar [as 别名]
def get_response(self, query):
response = None
try:
# we can't use helpers.retrieve_url because of redirects
# we need the cookie processor to handle redirects
opener = urllib_request.build_opener(urllib_request.HTTPCookieProcessor(CookieJar()))
response = opener.open(query).read().decode('utf-8')
except urllib_request.HTTPError as e:
# if the page returns a magnet redirect, used in download_torrent
if e.code == 302:
response = e.url
except Exception:
pass
return response
示例12: __init__
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import CookieJar [as 别名]
def __init__(self, host, port, timeout=120, ssl=False, opener=None):
self._root_url = "{http}{host}:{port}".format(
http=(ssl and "https://" or "http://"), host=host, port=port
)
self._timeout = timeout
self._builder = URLBuilder(self)
self._opener = opener
if not opener:
cookie_jar = CookieJar()
self._opener = build_opener(HTTPCookieProcessor(cookie_jar))
示例13: addcookies
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import CookieJar [as 别名]
def addcookies(self, cookies):
"""Inject Cookies from a CookieJar into our JSON dictionary."""
if not isinstance(cookies, RequestsCookieJar):
return False
for domain, pathdict in cookies._cookies.items():
search_ip = IP_REGEX.match(domain)
if search_ip:
# Match either an IPv4 address or an IPv6 address with a local suffix
domain_key = search_ip.group("ip")
else:
domain_key = domain if domain[0] == '.' else '.' + domain
if domain_key not in self.cookiedict.keys():
self.cookiedict[domain_key] = {}
for path, keydict in pathdict.items():
if path not in self.cookiedict[domain_key].keys():
self.cookiedict[domain_key][path] = {}
for key, cookieobj in keydict.items():
if isinstance(cookieobj, Cookie):
print(cookieobj)
cookie_attrs = {
"value": cookieobj.value,
"expires": cookieobj.expires,
"secure": cookieobj.secure,
"port": cookieobj.port,
"version": cookieobj.version
}
self.cookiedict[domain_key][path][key] = cookie_attrs
示例14: load
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import CookieJar [as 别名]
def load(self):
'''Load cookies into a cookiejar'''
cookie_jar = cookielib.CookieJar()
for cookie in self.get_cookies():
cookie_jar.set_cookie(cookie)
return cookie_jar
示例15: nse_opener
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import CookieJar [as 别名]
def nse_opener(self):
"""
builds opener for urllib2
:return: opener object
"""
cj = CookieJar()
return build_opener(HTTPCookieProcessor(cj))