本文整理汇总了Python中requests.utils方法的典型用法代码示例。如果您正苦于以下问题:Python requests.utils方法的具体用法?Python requests.utils怎么用?Python requests.utils使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests
的用法示例。
在下文中一共展示了requests.utils方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _session_check
# 需要导入模块: import requests [as 别名]
# 或者: from requests import utils [as 别名]
def _session_check(self):
"""Attempt to authenticate the user through a session file.
This process is done to avoid having to authenticate the user every
single time. It uses a session file that is saved when a valid session
is captured and then reused. Because sessions can expire, we need to
test the session prior to calling the user authenticated. Right now
that is done with a test string found in an unauthenticated session.
This approach is not an ideal method, but it works.
"""
if not os.path.exists(SESSION_FILE):
self._log.debug("Session file does not exist")
return False
with open(SESSION_FILE, 'rb') as f:
cookies = requests.utils.cookiejar_from_dict(pickle.load(f))
self._session.cookies = cookies
self._log.debug("Loaded cookies from session file")
response = self._session.get(url=self.TEST_URL, headers=self.HEADERS)
if self.TEST_KEY in str(response.content):
self._log.debug("Session file appears invalid")
return False
self._is_authenticated = True
self._process_state()
return True
示例2: authenticated
# 需要导入模块: import requests [as 别名]
# 或者: from requests import utils [as 别名]
def authenticated(func):
def wrapper(self, *args, **kwargs):
success = False
# 先判断有没有cookie文件, 再判断cookie是否有效
if 'z_c0' in requests.utils.dict_from_cookiejar(self.cookies):
from ..url import URL
r = self._execute(method="get", url=URL.profile(user_slug="zhijun-liu"))
success = r.ok
while not success:
account = input("请输入Email或者手机号码:")
password = input("请输入密码:")
obj = Account()
data = obj.login(account, password)
if "error" not in data:
success = True
self.cookies = obj.cookies
else:
print(data["error"]["message"])
else:
return func(self, *args, **kwargs)
return wrapper
示例3: _search
# 需要导入模块: import requests [as 别名]
# 或者: from requests import utils [as 别名]
def _search(self, limit, format):
'''
Returns a list of result objects, with the url for the next page bing search url.
'''
url = self.QUERY_URL.format(requests.utils.quote("'{}'".format(self.query)), min(50, limit), self.current_offset, format)
r = requests.get(url, auth=("", self.api_key))
try:
json_results = r.json()
except ValueError as vE:
if not self.safe:
raise PyBingWebException("Request returned with code %s, error msg: %s" % (r.status_code, r.text))
else:
print ("[ERROR] Request returned with code %s, error msg: %s. \nContinuing in 5 seconds." % (r.status_code, r.text))
time.sleep(5)
packaged_results = [WebResult(single_result_json) for single_result_json in json_results['d']['results']]
self.current_offset += min(50, limit, len(packaged_results))
return packaged_results
示例4: copy_session
# 需要导入模块: import requests [as 别名]
# 或者: from requests import utils [as 别名]
def copy_session(session: requests.Session, request_timeout: Optional[float] = None) -> requests.Session:
"""Duplicates a requests.Session."""
new = requests.Session()
new.cookies = requests.utils.cookiejar_from_dict(requests.utils.dict_from_cookiejar(session.cookies))
new.headers = session.headers.copy() # type: ignore
if request_timeout is not None:
# Override default timeout behavior.
# Need to silence mypy bug for this. See: https://github.com/python/mypy/issues/2427
new.request = partial(new.request, timeout=request_timeout) # type: ignore
return new
示例5: save_session_to_file
# 需要导入模块: import requests [as 别名]
# 或者: from requests import utils [as 别名]
def save_session_to_file(self, sessionfile):
"""Not meant to be used directly, use :meth:`Instaloader.save_session_to_file`."""
pickle.dump(requests.utils.dict_from_cookiejar(self._session.cookies), sessionfile)
示例6: load_session_from_file
# 需要导入模块: import requests [as 别名]
# 或者: from requests import utils [as 别名]
def load_session_from_file(self, username, sessionfile):
"""Not meant to be used directly, use :meth:`Instaloader.load_session_from_file`."""
session = requests.Session()
session.cookies = requests.utils.cookiejar_from_dict(pickle.load(sessionfile))
session.headers.update(self._default_http_header())
session.headers.update({'X-CSRFToken': session.cookies.get_dict()['csrftoken']})
if self.request_timeout is not None:
# Override default timeout behavior.
# Need to silence mypy bug for this. See: https://github.com/python/mypy/issues/2427
session.request = partial(session.request, timeout=self.request_timeout) # type: ignore
self._session = session
self.username = username
示例7: authenticate
# 需要导入模块: import requests [as 别名]
# 或者: from requests import utils [as 别名]
def authenticate(self):
"""Authenticate the user and setup our state."""
valid = self._session_check()
if self._is_authenticated and valid:
self._log.debug("[!] User has already authenticated")
return
init = self._session.get(url=self.LOGIN_URL, headers=self.HEADERS)
soup = BeautifulSoup(init.content, "html.parser")
soup_login = soup.find('form').find_all('input')
post_data = dict()
for u in soup_login:
if u.has_attr('name') and u.has_attr('value'):
post_data[u['name']] = u['value']
post_data['Email'] = self._email
post_data['Passwd'] = self._password
response = self._session.post(url=self.AUTH_URL, data=post_data,
headers=self.HEADERS)
if self.CAPTCHA_KEY in str(response.content):
raise AccountCaptcha('Google is forcing a CAPTCHA. To get around this issue, run the google-alerts with the seed option to open an interactive authentication session. Once authenticated, this module will cache your session and load that in the future')
cookies = [x.name for x in response.cookies]
if 'SIDCC' not in cookies:
raise InvalidCredentials("Email or password was incorrect.")
with open(SESSION_FILE, 'wb') as f:
cookies = requests.utils.dict_from_cookiejar(self._session.cookies)
pickle.dump(cookies, f, protocol=2)
self._log.debug("Saved session to disk for future reference")
self._log.debug("User successfully authenticated")
self._is_authenticated = True
self._process_state()
return
示例8: load_cookie
# 需要导入模块: import requests [as 别名]
# 或者: from requests import utils [as 别名]
def load_cookie(cls):
cookies = None
if os.path.exists(config.COOKIE_FILE):
with open(config.COOKIE_FILE) as fp:
try:
cookies = requests.utils.cookiejar_from_dict(json.load(fp))
logger.info('load cookies from {filename}'.format(filename=config.COOKIE_FILE))
except json.decoder.JSONDecodeError:
cookies = None
return cookies
示例9: dump_cookie
# 需要导入模块: import requests [as 别名]
# 或者: from requests import utils [as 别名]
def dump_cookie(self):
cookies = self.session.cookies
for cookie in cookies:
if cookie.name == 't' and cookie.path != '/':
cookies.clear(cookie.domain, cookie.path, cookie.name)
with open(config.COOKIE_FILE, 'w') as fp:
json.dump(requests.utils.dict_from_cookiejar(cookies), fp)
示例10: _acquire_authentication_cookie
# 需要导入模块: import requests [as 别名]
# 或者: from requests import utils [as 别名]
def _acquire_authentication_cookie(self, security_token, federated=False):
"""Retrieve auth cookie from STS
:type federated: bool
:type security_token: str
"""
logger = self.logger(self._acquire_authentication_cookie.__name__)
session = requests.session()
logger.debug_secrets("session: %s\nsession.post(%s, data=%s)", session, self.__sts_profile.signin_page_url,
security_token)
if not federated:
self._auth_cookies['FedAuth'] = None
self._auth_cookies['rtFa'] = None
session.post(self.__sts_profile.signin_page_url, data=security_token,
headers={'Content-Type': 'application/x-www-form-urlencoded'})
else:
self._auth_cookies['SPOIDCRL'] = None
session.get(self.__sts_profile.signin_page_url,
headers={
'User-Agent': 'Office365 Python Client',
'X-IDCRL_ACCEPTED': 't',
'Authorization': 'BPOSIDCRL {0}'.format(security_token)
})
logger.debug_secrets("session.cookies: %s", session.cookies)
cookies = requests.utils.dict_from_cookiejar(session.cookies)
logger.debug_secrets("cookies: %s", cookies)
if not cookies:
self.error = "An error occurred while retrieving auth cookies from {0}".format(
self.__sts_profile.signin_page_url)
logger.error(self.error)
return False
for name in self._auth_cookies.keys():
self._auth_cookies[name] = cookies[name]
return True