Python requests.cookies() Examples
The following are 30 code examples for showing how to use requests.cookies(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
requests
, or try the search function
.
Example 1
Project: py3-pinterest Author: bstoilov File: Pinterest.py License: MIT License | 6 votes |
def __init__(self, password='', proxies=None, username='', email='', cred_root='data', user_agent=None): self.email = email self.username = username self.password = password self.req_builder = RequestBuilder() self.bookmark_manager = BookmarkManager() self.http = requests.session() self.proxies = proxies self.user_agent = user_agent data_path = os.path.join(cred_root, self.email) + os.sep if not os.path.isdir(data_path): os.makedirs(data_path) self.registry = Registry('{}registry.dat'.format(data_path)) cookies = self.registry.get(Registry.Key.COOKIES) if cookies is not None: self.http.cookies.update(cookies) if self.user_agent is None: self.user_agent = AGENT_STRING
Example 2
Project: py3-pinterest Author: bstoilov File: Pinterest.py License: MIT License | 6 votes |
def request(self, method, url, data=None, files=None, extra_headers=None): headers = CaseInsensitiveDict([ ('Referer', HOME_PAGE), ('X-Requested-With', 'XMLHttpRequest'), ('Accept', 'application/json'), ('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'), ('User-Agent', self.user_agent)]) csrftoken = self.http.cookies.get('csrftoken') if csrftoken: headers.update([('X-CSRFToken', csrftoken)]) if extra_headers is not None: for h in extra_headers: headers.update([(h, extra_headers[h])]) response = self.http.request(method, url, data=data, headers=headers, files=files, proxies=self.proxies) response.raise_for_status() self.registry.update(Registry.Key.COOKIES, response.cookies) return response
Example 3
Project: streamlink Author: streamlink File: plugin.py License: BSD 2-Clause "Simplified" License | 6 votes |
def load_cookies(self): """ Load any stored cookies for the plugin that have not expired. :return: list of the restored cookie names """ if not self.session or not self.cache: raise RuntimeError("Cannot loaded cached cookies in unbound plugin") restored = [] for key, value in self.cache.get_all().items(): if key.startswith("__cookie"): cookie = requests.cookies.create_cookie(**value) self.session.http.cookies.set_cookie(cookie) restored.append(cookie.name) if restored: self.logger.debug("Restored cookies: {0}".format(", ".join(restored))) return restored
Example 4
Project: streamlink Author: streamlink File: plugin.py License: BSD 2-Clause "Simplified" License | 6 votes |
def clear_cookies(self, cookie_filter=None): """ Removes all of the saved cookies for this Plugin. To filter the cookies that are deleted specify the ``cookie_filter`` argument (see :func:`save_cookies`). :param cookie_filter: a function to filter the cookies :type cookie_filter: function :return: list of the removed cookie names """ if not self.session or not self.cache: raise RuntimeError("Cannot loaded cached cookies in unbound plugin") cookie_filter = cookie_filter or (lambda c: True) removed = [] for key, value in sorted(self.cache.get_all().items(), key=operator.itemgetter(0), reverse=True): if key.startswith("__cookie"): cookie = requests.cookies.create_cookie(**value) if cookie_filter(cookie): del self.session.http.cookies[cookie.name] self.cache.set(key, None, 0) removed.append(key) return removed
Example 5
Project: 115wangpan Author: shichao-an File: api.py License: BSD 2-Clause "Simplified" License | 6 votes |
def _req_files_download_url(self, pickcode, proapi=False): if '_115_curtime' not in self.cookies: self._req_file_userfile() if not proapi: url = self.web_api_url + '/download' params = {'pickcode': pickcode, '_': get_timestamp(13)} else: url = self.proapi_url params = {'pickcode': pickcode, 'method': 'get_file_url'} headers = { 'Referer': self.referer_url, } req = Request(method='GET', url=url, params=params, headers=headers) res = self.http.send(req) if res.state: if not proapi: return res.content['file_url'] else: fid = res.content['data'].keys()[0] return res.content['data'][fid]['url']['url'] else: raise RequestFailure('Failed to get download URL.')
Example 6
Project: infrared Author: redhat-openstack File: beaker_provisioner.py License: Apache License 2.0 | 6 votes |
def create_session(self): """ Creates a session and get some details on the machine. """ self.session = requests.Session() self.session.auth = self.auth # whether verify the SSL certificate or not self.session.verify = self.ca_cert or False if self.disable_warnings: requests.packages.urllib3.disable_warnings() if self.web_service == 'rpc': self.session.cookies = requests.cookies.cookiejar_from_dict( self._xml_rpc_auth()) else: # cookie workaround - consider changing with _get_last_activity() self.list_systems(limit=1) self.details = self.get_system_details()
Example 7
Project: YoudaoNoteExport Author: wesley2012 File: main.py License: MIT License | 6 votes |
def login(self, username, password): self.get('https://note.youdao.com/web/') self.headers['Referer'] = 'https://note.youdao.com/web/' self.get('https://note.youdao.com/signIn/index.html?&callback=https%3A%2F%2Fnote.youdao.com%2Fweb%2F&from=web') self.headers['Referer'] = 'https://note.youdao.com/signIn/index.html?&callback=https%3A%2F%2Fnote.youdao.com%2Fweb%2F&from=web' self.get('https://note.youdao.com/login/acc/pe/getsess?product=YNOTE&_=' + timestamp()) self.get('https://note.youdao.com/auth/cq.json?app=web&_=' + timestamp()) self.get('https://note.youdao.com/auth/urs/login.json?app=web&_=' + timestamp()) data = { "username": username, "password": hashlib.md5(password).hexdigest() } self.post('https://note.youdao.com/login/acc/urs/verify/check?app=web&product=YNOTE&tp=urstoken&cf=6&fr=1&systemName=&deviceType=&ru=https%3A%2F%2Fnote.youdao.com%2FsignIn%2F%2FloginCallback.html&er=https%3A%2F%2Fnote.youdao.com%2FsignIn%2F%2FloginCallback.html&vcode=&systemName=&deviceType=×tamp=' + timestamp(), data=data, allow_redirects=True) self.get('https://note.youdao.com/yws/mapi/user?method=get&multilevelEnable=true&_=' + timestamp()) print(self.cookies) self.cstk = self.cookies.get('YNOTE_CSTK')
Example 8
Project: tavern Author: taverntesting File: request.py License: MIT License | 6 votes |
def _set_cookies_for_request(session, request_args): """ Possibly reset session cookies for a single request then set them back. If no cookies were present in the request arguments, do nothing. This does not use try/finally because if it fails then we don't care about the cookies anyway Args: session (requests.Session): Current session request_args (dict): current request arguments """ if "cookies" in request_args: old_cookies = dict_from_cookiejar(session.cookies) session.cookies = cookiejar_from_dict({}) yield session.cookies = cookiejar_from_dict(old_cookies) else: yield
Example 9
Project: wechat_articles_spider Author: wnma3mz File: ArticlesUrls.py License: Apache License 2.0 | 6 votes |
def __save_cookie(self, username): """ 存储cookies, username用于文件命名 Parameters ---------- username: str 用户账号 Returns ------- None """ #实例化一个LWPcookiejar对象 new_cookie_jar = cookielib.LWPCookieJar(username + '.txt') #将转换成字典格式的RequestsCookieJar(这里我用字典推导手动转的)保存到LWPcookiejar中 requests.utils.cookiejar_from_dict( {c.name: c.value for c in self.s.cookies}, new_cookie_jar) #保存到本地文件 new_cookie_jar.save('cookies/' + username + '.txt', ignore_discard=True, ignore_expires=True)
Example 10
Project: wechat_articles_spider Author: wnma3mz File: ArticlesUrls.py License: Apache License 2.0 | 6 votes |
def __read_cookie(self, username): """ 读取cookies, username用于文件命名 Parameters ---------- username: str 用户账号 Returns ------- None """ #实例化一个LWPCookieJar对象 load_cookiejar = cookielib.LWPCookieJar() #从文件中加载cookies(LWP格式) load_cookiejar.load('cookies/' + username + '.txt', ignore_discard=True, ignore_expires=True) #工具方法转换成字典 load_cookies = requests.utils.dict_from_cookiejar(load_cookiejar) #工具方法将字典转换成RequestsCookieJar,赋值给session的cookies. self.s.cookies = requests.utils.cookiejar_from_dict(load_cookies)
Example 11
Project: Panda-Learning Author: Alivon File: score.py License: GNU Lesser General Public License v3.0 | 6 votes |
def get_score(cookies): try: jar = RequestsCookieJar() for cookie in cookies: jar.set(cookie['name'], cookie['value']) total = requests.get("https://pc-api.xuexi.cn/open/api/score/get", cookies=jar).content.decode("utf8") total = int(json.loads(total, encoding="utf8")["data"]["score"]) each = requests.get("https://pc-api.xuexi.cn/open/api/score/today/queryrate", cookies=jar).content.decode( "utf8") each = json.loads(each, encoding="utf8")["data"]["dayScoreDtos"] each = [int(i["currentScore"]) for i in each if i["ruleId"] in [1, 2, 9, 1002, 1003]] return total, each except: print("=" * 120) print("get_video_links获取失败") print("=" * 120) raise
Example 12
Project: streamlink Author: streamlink File: plugin.py License: BSD 2-Clause "Simplified" License | 5 votes |
def save_cookies(self, cookie_filter=None, default_expires=60 * 60 * 24 * 7): """ Store the cookies from ``http`` in the plugin cache until they expire. The cookies can be filtered by supplying a filter method. eg. ``lambda c: "auth" in c.name``. If no expiry date is given in the cookie then the ``default_expires`` value will be used. :param cookie_filter: a function to filter the cookies :type cookie_filter: function :param default_expires: time (in seconds) until cookies with no expiry will expire :type default_expires: int :return: list of the saved cookie names """ if not self.session or not self.cache: raise RuntimeError("Cannot cache cookies in unbound plugin") cookie_filter = cookie_filter or (lambda c: True) saved = [] for cookie in filter(cookie_filter, self.session.http.cookies): cookie_dict = {} for attr in ("version", "name", "value", "port", "domain", "path", "secure", "expires", "discard", "comment", "comment_url", "rfc2109"): cookie_dict[attr] = getattr(cookie, attr, None) cookie_dict["rest"] = getattr(cookie, "rest", getattr(cookie, "_rest", None)) expires = default_expires if cookie_dict['expires']: expires = int(cookie_dict['expires'] - time.time()) key = "__cookie:{0}:{1}:{2}:{3}".format(cookie.name, cookie.domain, cookie.port_specified and cookie.port or "80", cookie.path_specified and cookie.path or "*") self.cache.set(key, cookie_dict, expires) saved.append(cookie.name) if saved: self.logger.debug("Saved cookies: {0}".format(", ".join(saved))) return saved
Example 13
Project: zhihu-python Author: egrcc File: auth.py License: MIT License | 5 votes |
def login(account=None, password=None): if islogin() == True: Logging.success(u"你已经登录过咯") return True if account == None: (account, password) = read_account_from_config_file() if account == None: sys.stdout.write(u"请输入登录账号: ") account = raw_input() password = getpass("请输入登录密码: ") form_data = build_form(account, password) """ result: {"result": True} {"error": {"code": 19855555, "message": "unknown.", "data": "data" } } {"error": {"code": -1, "message": u"unknown error"} } """ result = upload_form(form_data) if "error" in result: if result["error"]['code'] == 1991829: # 验证码错误 Logging.error(u"验证码输入错误,请准备重新输入。" ) return login() elif result["error"]['code'] == 100005: # 密码错误 Logging.error(u"密码输入错误,请准备重新输入。" ) return login() else: Logging.warn(u"unknown error." ) return False elif "result" in result and result['result'] == True: # 登录成功 Logging.success(u"登录成功!" ) requests.cookies.save() return True
Example 14
Project: galaxy_blizzard_plugin Author: bartok765 File: http_client.py License: MIT License | 5 votes |
def parse_cookies(self, cookies): if not self.region: self.region = _found_region(cookies) new_cookies = {cookie["name"]: cookie["value"] for cookie in cookies} return requests.cookies.cookiejar_from_dict(new_cookies)
Example 15
Project: galaxy_blizzard_plugin Author: bartok765 File: http_client.py License: MIT License | 5 votes |
def create_session(self): self.session = requests.Session() self.session.cookies = self.auth_data.cookie_jar self.region = self.auth_data.region self.session.max_redirects = 300 self.session.headers = { "Authorization": f"Bearer {self.auth_data.access_token}", "User-Agent": FIREFOX_AGENT }
Example 16
Project: galaxy_blizzard_plugin Author: bartok765 File: http_client.py License: MIT License | 5 votes |
def refresh_credentials(self): creds = { "cookie_jar": pickle.dumps(self.session.cookies).hex(), "access_token": self.auth_data.access_token, "region": self.auth_data.region, "user_details_cache": self.user_details } self._plugin.store_credentials(creds)
Example 17
Project: galaxy_blizzard_plugin Author: bartok765 File: plugin.py License: MIT License | 5 votes |
def pass_login_credentials(self, step, credentials, cookies): if "logout&app=oauth" in credentials['end_uri']: # 2fa expired, repeat authentication return self.authentication_client.authenticate_using_login() if self.authentication_client.attempted_to_set_battle_tag: self.authentication_client.user_details = await self.backend_client.get_user_info() return self.authentication_client.parse_auth_after_setting_battletag() cookie_jar = self.authentication_client.parse_cookies(cookies) auth_data = await self.authentication_client.get_auth_data_login(cookie_jar, credentials) try: await self.authentication_client.create_session() await self.backend_client.refresh_cookies() except (BackendNotAvailable, BackendError, NetworkError, UnknownError, BackendTimeout) as e: raise e except Exception: raise InvalidCredentials() auth_status = await self.backend_client.validate_access_token(auth_data.access_token) if not ("authorities" in auth_status and "IS_AUTHENTICATED_FULLY" in auth_status["authorities"]): raise InvalidCredentials() self.authentication_client.user_details = await self.backend_client.get_user_info() self.authentication_client.set_credentials() return self.authentication_client.parse_battletag()
Example 18
Project: sseclient Author: btubbs File: test_sseclient.py License: MIT License | 5 votes |
def test_client_sends_cookies(): s = requests.Session() s.cookies = RequestsCookieJar() s.cookies['foo'] = 'bar' with mock.patch('sseclient.requests.Session.send') as m: m.return_value.encoding = "utf-8" sseclient.SSEClient('http://blah.com', session=s) prepared_request = m.call_args[0][0] assert prepared_request.headers['Cookie'] == 'foo=bar'
Example 19
Project: 115wangpan Author: shichao-an File: api.py License: BSD 2-Clause "Simplified" License | 5 votes |
def __init__(self, persistent=False, cookies_filename=None, cookies_type='LWPCookieJar'): """ :param bool auto_logout: whether to logout automatically when :class:`.API` object is destroyed .. deprecated:: 0.6.0 Call :meth:`.API.logout` explicitly :param bool persistent: whether to use persistent session that stores cookies on disk :param str cookies_filename: path to the cookies file, use default path (`~/.115cookies`) if None :param str cookies_type: a string representing :class:`cookielib.FileCookieJar` subclass, `LWPCookieJar` (default) or `MozillaCookieJar` """ self.persistent = persistent self.cookies_filename = cookies_filename self.cookies_type = cookies_type self.passport = None self.http = RequestHandler() self.logger = logging.getLogger(conf.LOGGING_API_LOGGER) # Cache attributes to decrease API hits self._user_id = None self._username = None self._signatures = {} self._upload_url = None self._lixian_timestamp = None self._root_directory = None self._downloads_directory = None self._receiver_directory = None self._torrents_directory = None self._task_count = None self._task_quota = None if self.persistent: self.load_cookies()
Example 20
Project: 115wangpan Author: shichao-an File: api.py License: BSD 2-Clause "Simplified" License | 5 votes |
def _init_cookies(self): # RequestsLWPCookieJar or RequestsMozillaCookieJar cookies_class = globals()['Requests' + self.cookies_type] f = self.cookies_filename or conf.COOKIES_FILENAME self.cookies = cookies_class(f)
Example 21
Project: 115wangpan Author: shichao-an File: api.py License: BSD 2-Clause "Simplified" License | 5 votes |
def load_cookies(self, ignore_discard=True, ignore_expires=True): """Load cookies from the file :attr:`.API.cookies_filename`""" self._init_cookies() if os.path.exists(self.cookies.filename): self.cookies.load(ignore_discard=ignore_discard, ignore_expires=ignore_expires) self._reset_cache()
Example 22
Project: 115wangpan Author: shichao-an File: api.py License: BSD 2-Clause "Simplified" License | 5 votes |
def save_cookies(self, ignore_discard=True, ignore_expires=True): """Save cookies to the file :attr:`.API.cookies_filename`""" if not isinstance(self.cookies, cookielib.FileCookieJar): m = 'Cookies must be a cookielib.FileCookieJar object to be saved.' raise APIError(m) self.cookies.save(ignore_discard=ignore_discard, ignore_expires=ignore_expires)
Example 23
Project: 115wangpan Author: shichao-an File: api.py License: BSD 2-Clause "Simplified" License | 5 votes |
def cookies(self): """ Cookies of the current API session (cookies getter shortcut) """ return self.http.session.cookies
Example 24
Project: 115wangpan Author: shichao-an File: api.py License: BSD 2-Clause "Simplified" License | 5 votes |
def _get_username(self): return unquote(self.cookies.get('OOFL'))
Example 25
Project: gs-quant Author: goldmansachs File: session.py License: Apache License 2.0 | 5 votes |
def _headers(self): return [('Cookie', 'GSSSO=' + self._session.cookies['GSSSO'])]
Example 26
Project: PatentCrawler Author: will4906 File: account.py License: Apache License 2.0 | 5 votes |
def get_captcha(): """ 获取验证码 :return: """ resp = requests.get(url=url_captcha.get('url'), cookies=ctrl.COOKIES, proxies=ctrl.PROXIES) with open('captcha.png', 'wb') as f: f.write(resp.content) result = get_captcha_result(CAPTCHA_MODEL_NAME, 'captcha.png') return result
Example 27
Project: tavern Author: taverntesting File: test_request.py License: MIT License | 5 votes |
def test_bad_get_body(self, req, includes): """Can't add a body with a GET request """ req["method"] = "GET" with pytest.warns(RuntimeWarning): RestRequest( Mock(spec=requests.Session, cookies=RequestsCookieJar()), req, includes )
Example 28
Project: tavern Author: taverntesting File: test_request.py License: MIT License | 5 votes |
def mock_session(self): return Mock(spec=requests.Session, cookies=RequestsCookieJar())
Example 29
Project: tavern Author: taverntesting File: test_request.py License: MIT License | 5 votes |
def test_no_expected_none_available(self, mock_session, req, includes): """No cookies expected and none available = OK""" req["cookies"] = [] assert _read_expected_cookies(mock_session, req, includes) == {}
Example 30
Project: tavern Author: taverntesting File: test_request.py License: MIT License | 5 votes |
def test_available_not_waited(self, req, includes): """some available but not set""" cookiejar = RequestsCookieJar() cookiejar.set("a", 2) mock_session = Mock(spec=requests.Session, cookies=cookiejar) assert _read_expected_cookies(mock_session, req, includes) == None