Python requests.cookies.RequestsCookieJar() Examples
The following are 28
code examples of requests.cookies.RequestsCookieJar().
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.cookies
, or try the search function
.
Example #1
Source File: http_client_test.py From rets with MIT License | 7 votes |
def test_cookie_dict(): c = RetsHttpClient('login_url', 'username', 'password') c._session = mock.MagicMock() jar = RequestsCookieJar() c1 = Cookie(1, 'name1', 'value1', 80, 80, 'domain', 'domain_specified', 'domain_initial_dot', 'path', 'path_specified', True, True, False, 'comment', 'comment_url', 'rest') c2 = Cookie(1, 'name2', 'value2', 80, 80, 'domain', 'domain_specified', 'domain_initial_dot', 'path', 'path_specified', True, True, False, 'comment', 'comment_url', 'rest') c3 = Cookie(1, 'name1', 'value1', 80, 80, 'domain', 'domain_specified3', 'domain_initial_dot3', 'path3', 'path_specified3', True, True, False, 'comment', 'comment_url', 'rest') jar.set_cookie(c1) jar.set_cookie(c2) jar.set_cookie(c3) c._session.cookies = jar assert c.cookie_dict == {'name1': 'value1', 'name2': 'value2'}
Example #2
Source File: yahoo.py From yahoo-group-archiver with MIT License | 7 votes |
def init_cookie_jar(cookie_file=None, cookie_t=None, cookie_y=None, cookie_euconsent=None): cookie_jar = LWPCookieJar(cookie_file) if cookie_file else RequestsCookieJar() if cookie_file and os.path.exists(cookie_file): cookie_jar.load(ignore_discard=True) if args.cookie_t: cookie_jar.set_cookie(create_cookie('T', cookie_t)) if cookie_y: cookie_jar.set_cookie(create_cookie('Y', cookie_y)) if cookie_euconsent: cookie_jar.set_cookie(create_cookie('EuConsent', cookie_euconsent)) if cookie_file: cookie_jar.save(ignore_discard=True) return cookie_jar
Example #3
Source File: guess.py From bazarr with GNU General Public License v3.0 | 6 votes |
def _handle_basic_auth_407(self, r, kwargs): if self.pos is not None: r.request.body.seek(self.pos) r.content r.raw.release_conn() prep = r.request.copy() if not hasattr(prep, '_cookies'): prep._cookies = cookies.RequestsCookieJar() cookies.extract_cookies_to_jar(prep._cookies, r.request, r.raw) prep.prepare_cookies(prep._cookies) self.proxy_auth = auth.HTTPProxyAuth(self.proxy_username, self.proxy_password) prep = self.proxy_auth(prep) _r = r.connection.send(prep, **kwargs) _r.history.append(r) _r.request = prep return _r
Example #4
Source File: guess.py From bazarr with GNU General Public License v3.0 | 6 votes |
def _handle_basic_auth_401(self, r, kwargs): if self.pos is not None: r.request.body.seek(self.pos) # Consume content and release the original connection # to allow our new request to reuse the same one. r.content r.raw.release_conn() prep = r.request.copy() if not hasattr(prep, '_cookies'): prep._cookies = cookies.RequestsCookieJar() cookies.extract_cookies_to_jar(prep._cookies, r.request, r.raw) prep.prepare_cookies(prep._cookies) self.auth = auth.HTTPBasicAuth(self.username, self.password) prep = self.auth(prep) _r = r.connection.send(prep, **kwargs) _r.history.append(r) _r.request = prep return _r
Example #5
Source File: guess.py From Requester with MIT License | 6 votes |
def _handle_basic_auth_407(self, r, kwargs): if self.pos is not None: r.request.body.seek(self.pos) r.content r.raw.release_conn() prep = r.request.copy() if not hasattr(prep, '_cookies'): prep._cookies = cookies.RequestsCookieJar() cookies.extract_cookies_to_jar(prep._cookies, r.request, r.raw) prep.prepare_cookies(prep._cookies) self.proxy_auth = auth.HTTPProxyAuth(self.proxy_username, self.proxy_password) prep = self.proxy_auth(prep) _r = r.connection.send(prep, **kwargs) _r.history.append(r) _r.request = prep return _r
Example #6
Source File: guess.py From Requester with MIT License | 6 votes |
def _handle_basic_auth_401(self, r, kwargs): if self.pos is not None: r.request.body.seek(self.pos) # Consume content and release the original connection # to allow our new request to reuse the same one. r.content r.raw.release_conn() prep = r.request.copy() if not hasattr(prep, '_cookies'): prep._cookies = cookies.RequestsCookieJar() cookies.extract_cookies_to_jar(prep._cookies, r.request, r.raw) prep.prepare_cookies(prep._cookies) self.auth = auth.HTTPBasicAuth(self.username, self.password) prep = self.auth(prep) _r = r.connection.send(prep, **kwargs) _r.history.append(r) _r.request = prep return _r
Example #7
Source File: guess.py From addon with GNU General Public License v3.0 | 6 votes |
def _handle_basic_auth_407(self, r, kwargs): if self.pos is not None: r.request.body.seek(self.pos) r.content r.raw.release_conn() prep = r.request.copy() if not hasattr(prep, '_cookies'): prep._cookies = cookies.RequestsCookieJar() cookies.extract_cookies_to_jar(prep._cookies, r.request, r.raw) prep.prepare_cookies(prep._cookies) self.proxy_auth = auth.HTTPProxyAuth(self.proxy_username, self.proxy_password) prep = self.proxy_auth(prep) _r = r.connection.send(prep, **kwargs) _r.history.append(r) _r.request = prep return _r
Example #8
Source File: guess.py From addon with GNU General Public License v3.0 | 6 votes |
def _handle_basic_auth_401(self, r, kwargs): if self.pos is not None: r.request.body.seek(self.pos) # Consume content and release the original connection # to allow our new request to reuse the same one. r.content r.raw.release_conn() prep = r.request.copy() if not hasattr(prep, '_cookies'): prep._cookies = cookies.RequestsCookieJar() cookies.extract_cookies_to_jar(prep._cookies, r.request, r.raw) prep.prepare_cookies(prep._cookies) self.auth = auth.HTTPBasicAuth(self.username, self.password) prep = self.auth(prep) _r = r.connection.send(prep, **kwargs) _r.history.append(r) _r.request = prep return _r
Example #9
Source File: cookies.py From bdbag with Apache License 2.0 | 6 votes |
def load_and_merge_cookie_jars(cookie_jar_paths): cookie_jar = RequestsCookieJar() if not cookie_jar_paths: return cookie_jar logging.debug("Attempting to load and merge the following cookie files: %s" % cookie_jar_paths) for f in cookie_jar_paths: if os.path.isfile(f): try: cookies = MozillaCookieJar(f) cookies.load(ignore_expires=True, ignore_discard=True) cookie_jar.update(cookies) except Exception as e: logging.warning("Unable to load cookie file [%s]: %s" % (f, get_typed_exception(e))) # Do not preserve expire values from cookies with expires=0 from the file, or requests will not use the cookie for cookie in iter(cookie_jar): if not cookie.expires: cookie.expires = None return cookie_jar
Example #10
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 #11
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 #12
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 #13
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 #14
Source File: test_request.py From tavern with MIT License | 5 votes |
def test_ask_for_nothing(self, req, includes): """explicitly ask fo rno cookies""" cookiejar = RequestsCookieJar() cookiejar.set("a", 2) mock_session = Mock(spec=requests.Session, cookies=cookiejar) req["cookies"] = [] assert _read_expected_cookies(mock_session, req, includes) == {}
Example #15
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 #16
Source File: test_request.py From tavern with MIT License | 5 votes |
def mock_session(self): return Mock(spec=requests.Session, cookies=RequestsCookieJar())
Example #17
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 #18
Source File: NetflixSession.py From plugin.video.netflix with MIT License | 5 votes |
def _load_cookies(self, filename): """ Loads cookies into the active session from a given file :param filename: path incl. filename of the cookie file :type filename: str :returns: bool or tuple -- Loading didn't work or parsed cookie data """ # check if we have in memory cookies to spare some file i/o current_cookie = self.parsed_cookies.get(filename, None) if current_cookie is not None: self.nx_common.log(msg='Loading cookies from memory') self.session.cookies = current_cookie[1] return current_cookie # return if we haven't found a cookie file if not os.path.isfile(filename): self.nx_common.log(msg='No cookies found') return False # open the cookies file & set the loaded cookies with open(filename, 'rb') as f: self.nx_common.log(msg='Loading cookies from file') _cookies = pickle.load(f) if _cookies: jar = cookies.RequestsCookieJar() jar._cookies = _cookies self.session.cookies = jar self.parsed_cookies[filename] = (_cookies, jar) return self.parsed_cookies.get(filename) else: return False
Example #19
Source File: test_browser.py From MechanicalSoup with MIT License | 5 votes |
def test_set_cookiejar(httpbin): """Set cookies locally and test that they are received remotely.""" # construct a phony cookiejar and attach it to the session jar = RequestsCookieJar() jar.set('field', 'value') assert jar.get('field') == 'value' browser = mechanicalsoup.Browser() browser.set_cookiejar(jar) resp = browser.get(httpbin + "/cookies") assert resp.json() == {'cookies': {'field': 'value'}}
Example #20
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'
Example #21
Source File: test_yahoogroupsapi.py From yahoo-group-archiver with MIT License | 5 votes |
def cookies(): cookies = RequestsCookieJar() cookies.set('T', 't_cookie') cookies.set('Y', 'y_cookie') cookies.set('EuConsent', 'eu_cookie') yield cookies
Example #22
Source File: jsoncookie.py From ITWSV with MIT License | 5 votes |
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
Example #23
Source File: cassettes.py From schemathesis with MIT License | 5 votes |
def get_prepared_request(data: Dict[str, Any]) -> requests.PreparedRequest: """Create a `requests.PreparedRequest` from a serialized one.""" prepared = requests.PreparedRequest() prepared.method = data["method"] prepared.url = data["uri"] prepared._cookies = RequestsCookieJar() # type: ignore encoded = data["body"]["base64_string"] if encoded: prepared.body = base64.b64decode(encoded) # There is always 1 value in a request headers = [(key, value[0]) for key, value in data["headers"].items()] prepared.headers = CaseInsensitiveDict(headers) return prepared
Example #24
Source File: wkvcwapi.py From PixivCrawlerIII with MIT License | 4 votes |
def _get_chrome_cookie(cache_path, url): '''Get chrome cookies with selenium @@API that allows external calls Due to the recaptcha mechanism set by the website, it is impossible to obtain the token using the normal method, and it is forced to adopt the webdriver of selenium. :param cache_path: local cookie cache text path :param url: selenium webdriver request url :return: cookie jar ''' cookie_jar = RequestsCookieJar() # first judge local cookie text file exist # if exists, just read it and return if os.path.exists(cache_path): dl.LT_PRINT('check local cookie file') with open(cache_path, "r") as fp: cookies = json.load(fp) # package to jar type for cookie in cookies: cookie_jar.set(cookie['name'], cookie['value']) return cookie_jar dl.LT_PRINT('start selenium webdriver') dl.LT_PRINT('target page: %s' % url) chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') # hide window chrome_options.add_argument('--disable-extensions') # disable chrome externsions chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--incognito') # seamless mode chrome_options.add_argument('--blink-settings=imagesEnabled=false') # do not load image ## chrome_options.add_argument('--start-maximized') chrome_options.add_argument('user-data-dir=' + os.path.abspath(dl.chrome_user_data_dir)) driver = webdriver.Chrome(chrome_options=chrome_options) # request website and get cookie driver.get(url) cookies = driver.get_cookies() driver.close() dl.LT_PRINT('stop selenium webdriver') # save cookies to file with open(cache_path, "w") as fp: json.dump(cookies, fp, sort_keys=True, indent=4) # package to jar type for cookie in cookies: cookie_jar.set(cookie['name'], cookie['value']) return cookie_jar
Example #25
Source File: __init__.py From w12scan-client with MIT License | 4 votes |
def session_request(self, method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=10, allow_redirects=True, proxies=None, hooks=None, stream=None, verify=False, cert=None, json=None): # Create the Request. merged_cookies = merge_cookies(merge_cookies(RequestsCookieJar(), self.cookies), cookies) default_header = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" } req = Request( method=method.upper(), url=url, headers=merge_setting(headers, default_header), files=files, data=data or {}, json=json, params=params or {}, auth=auth, cookies=merged_cookies, hooks=hooks, ) prep = self.prepare_request(req) proxies = proxies or {} settings = self.merge_environment_settings( prep.url, proxies, stream, verify, cert ) # Send the request. send_kwargs = { 'timeout': timeout, 'allow_redirects': allow_redirects, } send_kwargs.update(settings) resp = self.send(prep, **send_kwargs) if resp.encoding == 'ISO-8859-1': encodings = get_encodings_from_content(resp.text) if encodings: encoding = encodings[0] else: encoding = resp.apparent_encoding resp.encoding = encoding return resp
Example #26
Source File: utils.py From http-observatory with Mozilla Public License 2.0 | 4 votes |
def empty_requests(http_equiv_file=None) -> dict: req = { 'hostname': 'http-observatory.security.mozilla.org', 'resources': { '__path__': None, '/': None, '/clientaccesspolicy.xml': None, '/contribute.json': None, '/crossdomain.xml': None, '/robots.txt': None, }, 'responses': { 'auto': UserDict(), 'cors': None, 'http': None, 'https': None, }, 'session': UserDict(), } # Parse the HTML file for its own headers, if requested if http_equiv_file: __dirname = os.path.abspath(os.path.dirname(__file__)) with open(os.path.join(__dirname, 'unittests', 'files', http_equiv_file), 'r') as f: html = f.read() # Load the HTML file into the object for content tests. req['resources']['__path__'] = html req['responses']['auto'].headers = { 'Content-Type': 'text/html', } req['responses']['auto'].history = [] req['responses']['auto'].request = UserDict() req['responses']['auto'].request.headers = UserDict() req['responses']['auto'].status_code = 200 req['responses']['auto'].url = 'https://http-observatory.security.mozilla.org/' req['responses']['auto'].verified = True req['session'].cookies = RequestsCookieJar() req['responses']['cors'] = deepcopy(req['responses']['auto']) req['responses']['http'] = deepcopy(req['responses']['auto']) req['responses']['https'] = deepcopy(req['responses']['auto']) # Parse the HTML file for its own headers, if requested if http_equiv_file: req['responses']['auto'].http_equiv = parse_http_equiv_headers(req['resources']['__path__']) else: req['responses']['auto'].http_equiv = {} return req
Example #27
Source File: nsurlsession_adapter.py From python-jss with GNU General Public License v3.0 | 4 votes |
def build_response(req, delegate, cookiestore): # type: (PreparedRequest, NSURLSessionAdapterDelegate, NSHTTPCookieStorage) -> Response """Build a requests `Response` object from the response data collected by the NSURLSessionDelegate, and the default cookie store.""" response = Response() # Fallback to None if there's no status_code, for whatever reason. response.status_code = getattr(delegate, 'status', None) # Make headers case-insensitive. response.headers = CaseInsensitiveDict(getattr(delegate, 'headers', {})) # Set encoding. response.encoding = get_encoding_from_headers(response.headers) # response.raw = resp # response.reason = response.raw.reason if isinstance(req.url, bytes): response.url = req.url.decode('utf-8') else: response.url = req.url # Add new cookies from the server. For NSURLSession these have already been parsed. # jar = RequestsCookieJar() # for cookie in cookiestore.cookies(): # print cookie # c = Cookie( # version=cookie.version(), # name=cookie.name(), # value=cookie.value(), # port=8444, # # port=cookie.portList()[-1], # port_specified=0, # domain=cookie.domain(), # domain_specified=cookie.domain(), # domain_initial_dot=cookie.domain(), # path=cookie.path(), # path_specified=cookie.path(), # secure=!cookie.HTTPOnly(), # expires=cookie.expiresDate(), # comment=cookie.comment(), # comment_url=cookie.commentURL(), # ) # jar.set_cookie(c) # # response.cookies = jar response.raw = io.BytesIO(buffer(delegate.output)) # Give the Response some context. response.request = req # response.connection = self return response
Example #28
Source File: requests.py From gql with MIT License | 4 votes |
def __init__( self, url: str, headers: Optional[Dict[str, Any]] = None, cookies: Optional[Union[Dict[str, Any], RequestsCookieJar]] = None, auth: Optional[AuthBase] = None, use_json: bool = True, timeout: Optional[int] = None, verify: bool = True, retries: int = 0, method: str = "POST", **kwargs: Any ): """Initialize the transport with the given request parameters. :param url: The GraphQL server URL. :param headers: Dictionary of HTTP Headers to send with the :class:`Request` (Default: None). :param cookies: Dict or CookieJar object to send with the :class:`Request` (Default: None). :param auth: Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth (Default: None). :param use_json: Send request body as JSON instead of form-urlencoded (Default: True). :param timeout: Specifies a default timeout for requests (Default: None). :param verify: Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use. (Default: True). :param retries: Pre-setup of the requests' Session for performing retries :param method: HTTP method used for requests. (Default: POST). :param kwargs: Optional arguments that ``request`` takes. These can be seen at the :requests_: source code or the official :docs_: .. _requests: https://github.com/psf/requests/blob/master/requests/api.py .. _docs: https://requests.readthedocs.io/en/master/ """ self.url = url self.headers = headers self.cookies = cookies self.auth = auth self.use_json = use_json self.default_timeout = timeout self.verify = verify self.retries = retries self.method = method self.kwargs = kwargs self.session = None