Python requests.cookies() Examples
The following are 30
code examples of requests.cookies().
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 also want to check out all available functions/classes of the module
requests
, or try the search function
.
Example #1
Source File: Pinterest.py From py3-pinterest with MIT License | 8 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
Source File: api.py From 115wangpan with BSD 2-Clause "Simplified" License | 8 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 #3
Source File: Pinterest.py From py3-pinterest with MIT License | 7 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 #4
Source File: plugin.py From streamlink with BSD 2-Clause "Simplified" License | 7 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 #5
Source File: score.py From Panda-Learning with 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 #6
Source File: ArticlesUrls.py From wechat_articles_spider with 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 #7
Source File: ArticlesUrls.py From wechat_articles_spider with 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 #8
Source File: plugin.py From streamlink with 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 #9
Source File: beaker_provisioner.py From infrared with 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 #10
Source File: main.py From YoudaoNoteExport with 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 #11
Source File: request.py From tavern with 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 #12
Source File: account.py From PatentCrawler with 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 #13
Source File: api.py From 115wangpan with BSD 2-Clause "Simplified" License | 5 votes |
def _get_username(self): return unquote(self.cookies.get('OOFL'))
Example #14
Source File: test_request.py From tavern with 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 #15
Source File: test_request.py From tavern with MIT License | 5 votes |
def mock_session(self): return Mock(spec=requests.Session, cookies=RequestsCookieJar())
Example #16
Source File: test_request.py From tavern with 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 #17
Source File: test_request.py From tavern with 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
Example #18
Source File: test_request.py From tavern with MIT License | 5 votes |
def test_not_available_but_wanted(self, mock_session, req, includes): """Some wanted but not available""" req["cookies"] = ["a"] with pytest.raises(exceptions.MissingCookieError): _read_expected_cookies(mock_session, req, includes)
Example #19
Source File: test_request.py From tavern with MIT License | 5 votes |
def test_available_and_waited(self, req, includes): """some available and wanted""" cookiejar = RequestsCookieJar() cookiejar.set("a", 2) req["cookies"] = ["a"] mock_session = Mock(spec=requests.Session, cookies=cookiejar) assert _read_expected_cookies(mock_session, req, includes) == {"a": 2}
Example #20
Source File: test_request.py From tavern with MIT License | 5 votes |
def test_format_cookies(self, req, includes): """cookies in request should be formatted""" cookiejar = RequestsCookieJar() cookiejar.set("a", 2) req["cookies"] = ["{cookiename}"] includes["variables"]["cookiename"] = "a" mock_session = Mock(spec=requests.Session, cookies=cookiejar) assert _read_expected_cookies(mock_session, req, includes) == {"a": 2}
Example #21
Source File: test_request.py From tavern with MIT License | 5 votes |
def test_no_overwrite_cookie(self, req, includes): """cant redefine a cookie from previous request""" cookiejar = RequestsCookieJar() cookiejar.set("a", 2) req["cookies"] = ["a", {"a": "sjidfsd"}] mock_session = Mock(spec=requests.Session, cookies=cookiejar) with pytest.raises(exceptions.DuplicateCookieError): _read_expected_cookies(mock_session, req, includes)
Example #22
Source File: test_request.py From tavern with MIT License | 5 votes |
def test_no_duplicate_cookie(self, req, includes): """Can't override a cookiev alue twice""" cookiejar = RequestsCookieJar() req["cookies"] = [{"a": "sjidfsd"}, {"a": "fjhj"}] mock_session = Mock(spec=requests.Session, cookies=cookiejar) with pytest.raises(exceptions.DuplicateCookieError): _read_expected_cookies(mock_session, req, includes)
Example #23
Source File: session.py From gs-quant with Apache License 2.0 | 5 votes |
def _headers(self): return [('Cookie', 'GSSSO=' + self._session.cookies['GSSSO'])]
Example #24
Source File: plugin.py From streamlink with 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 #25
Source File: auth.py From zhihu-python with 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 #26
Source File: http_client.py From galaxy_blizzard_plugin with 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 #27
Source File: http_client.py From galaxy_blizzard_plugin with 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 #28
Source File: http_client.py From galaxy_blizzard_plugin with 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 #29
Source File: plugin.py From galaxy_blizzard_plugin with 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 #30
Source File: test_sseclient.py From sseclient with 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'