Python http.cookiejar.Cookie() Examples
The following are 29
code examples of http.cookiejar.Cookie().
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
http.cookiejar
, 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: util.py From gallery-dl with GNU General Public License v2.0 | 6 votes |
def save_cookiestxt(fp, cookies): """Write 'cookies' in Netscape cookies.txt format to 'fp'""" fp.write("# Netscape HTTP Cookie File\n\n") for cookie in cookies: if cookie.value is None: name = "" value = cookie.name else: name = cookie.name value = cookie.value fp.write("\t".join(( cookie.domain, "TRUE" if cookie.domain.startswith(".") else "FALSE", cookie.path, "TRUE" if cookie.secure else "FALSE", "0" if cookie.expires is None else str(cookie.expires), name, value, )) + "\n")
Example #3
Source File: test_headers.py From http-observatory with Mozilla Public License 2.0 | 6 votes |
def test_anticsrf_without_samesite(self): cookie = Cookie(name='CSRFTOKEN', comment=None, comment_url=None, discard=False, domain='mozilla.com', domain_initial_dot=False, domain_specified='mozilla.com', expires=None, path='/', path_specified='/', port=443, port_specified=443, rfc2109=False, rest={'HttpOnly': True}, secure=True, version=1, value='bar') self.reqs['session'].cookies.set_cookie(cookie) result = cookies(self.reqs) self.assertEquals('cookies-anticsrf-without-samesite-flag', result['result']) self.assertFalse(result['pass']) self.assertFalse(result['sameSite'])
Example #4
Source File: test_common.py From flask-security with MIT License | 6 votes |
def test_request_loader_does_not_fail_with_invalid_token(client): c = Cookie( version=0, name="remember_token", value="None", port=None, port_specified=False, domain="www.example.com", domain_specified=False, domain_initial_dot=False, path="/", path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={"HttpOnly": None}, rfc2109=False, ) client.cookie_jar.set_cookie(c) response = client.get("/") assert b"BadSignature" not in response.data
Example #5
Source File: test_headers.py From http-observatory with Mozilla Public License 2.0 | 6 votes |
def test_no_secure(self): cookie = Cookie(name='foo', comment=None, comment_url=None, discard=False, domain='mozilla.com', domain_initial_dot=False, domain_specified='mozilla.com', expires=None, path='/', path_specified='/', port=443, port_specified=443, rfc2109=False, rest={}, secure=False, version=1, value='bar') self.reqs['session'].cookies.set_cookie(cookie) result = cookies(self.reqs) self.assertEquals('cookies-without-secure-flag', result['result']) self.assertFalse(result['pass']) self.assertFalse(result['sameSite'])
Example #6
Source File: httptools.py From addon with GNU General Public License v3.0 | 6 votes |
def get_url_headers(url, forced=False): from . import scrapertools domain = urlparse.urlparse(url)[1] sub_dom = scrapertools.find_single_match(domain, '\.(.*?\.\w+)') if sub_dom and not 'google' in url: domain = sub_dom domain_cookies = cj._cookies.get("." + domain, {}).get("/", {}) if "|" in url or not "cf_clearance" in domain_cookies: if not forced: return url headers = dict() headers["User-Agent"] = default_headers["User-Agent"] headers["Cookie"] = "; ".join(["%s=%s" % (c.name, c.value) for c in list(domain_cookies.values())]) return url + "|" + "&".join(["%s=%s" % (h, urllib.quote(headers[h])) for h in headers])
Example #7
Source File: gocookies.py From python-libjuju with Apache License 2.0 | 6 votes |
def go_to_py_cookie(go_cookie): '''Convert a Go-style JSON-unmarshaled cookie into a Python cookie''' expires = None if go_cookie.get('Expires') is not None: t = pyrfc3339.parse(go_cookie['Expires']) expires = t.timestamp() return cookiejar.Cookie( version=0, name=go_cookie['Name'], value=go_cookie['Value'], port=None, port_specified=False, # Unfortunately Python cookies don't record the original # host that the cookie came from, so we'll just use Domain # for that purpose, and record that the domain was specified, # even though it probably was not. This means that # we won't correctly record the CanonicalHost entry # when writing the cookie file after reading it. domain=go_cookie['Domain'], domain_specified=not go_cookie['HostOnly'], domain_initial_dot=False, path=go_cookie['Path'], path_specified=True, secure=go_cookie['Secure'], expires=expires, discard=False, comment=None, comment_url=None, rest=None, rfc2109=False, )
Example #8
Source File: test_headers.py From http-observatory with Mozilla Public License 2.0 | 6 votes |
def test_samesite_invalid(self): cookie = Cookie(name='SESSIONID', comment=None, comment_url=None, discard=False, domain='mozilla.com', domain_initial_dot=False, domain_specified='mozilla.com', expires=None, path='/', path_specified='/', port=443, port_specified=443, rfc2109=False, rest={'HttpOnly': True, 'SameSite': 'Invalid'}, secure=True, version=1, value='bar') self.reqs['session'].cookies.set_cookie(cookie) result = cookies(self.reqs) self.assertEquals('cookies-samesite-flag-invalid', result['result']) self.assertFalse(result['pass']) self.assertIsNone(result['sameSite'])
Example #9
Source File: api.py From musicbox with MIT License | 6 votes |
def make_cookie(self, name, value): return Cookie( version=0, name=name, value=value, port=None, port_specified=False, domain="music.163.com", domain_specified=True, domain_initial_dot=False, path="/", path_specified=True, secure=False, expires=None, discard=False, comment=None, comment_url=None, rest={}, )
Example #10
Source File: test_headers.py From http-observatory with Mozilla Public License 2.0 | 5 votes |
def test_regular_cookie_no_secure_but_hsts(self): cookie = Cookie(name='foo', comment=None, comment_url=None, discard=False, domain='mozilla.com', domain_initial_dot=False, domain_specified='mozilla.com', expires=None, path='/', path_specified='/', port=443, port_specified=443, rfc2109=False, rest={}, secure=False, version=1, value='bar') self.reqs['session'].cookies.set_cookie(cookie) self.reqs['responses']['https'].headers['Strict-Transport-Security'] = 'max-age=15768000' result = cookies(self.reqs) self.assertEquals('cookies-without-secure-flag-but-protected-by-hsts', result['result']) self.assertFalse(result['pass']) self.assertFalse(result['sameSite'])
Example #11
Source File: util.py From gallery-dl with GNU General Public License v2.0 | 5 votes |
def load_cookiestxt(fp): """Parse a Netscape cookies.txt file and return a list of its Cookies""" cookies = [] for line in fp: line = line.lstrip() # strip '#HttpOnly_' if line.startswith("#HttpOnly_"): line = line[10:] # ignore empty lines and comments if not line or line[0] in ("#", "$"): continue # strip trailing '\n' if line[-1] == "\n": line = line[:-1] domain, domain_specified, path, secure, expires, name, value = \ line.split("\t") if not name: name = value value = None cookies.append(Cookie( 0, name, value, None, False, domain, domain_specified == "TRUE", domain.startswith("."), path, False, secure == "TRUE", None if expires == "0" or not expires else expires, False, None, None, {}, )) return cookies
Example #12
Source File: cookiejar.py From showroom with MIT License | 5 votes |
def cookie_from_dict(dct): """ Constructs a cookie from a dict. Fills in any missing parameters from a set of defaults. This method was based on Requests' "create_cookie" function, originally written by Miguel Turner (dhagrow): https://github.com/dhagrow/requests/blame/develop/requests/packages/oreos/cookiejar.py#L126 """ if 'name' not in dct or 'value' not in dct: raise TypeError('Cookie dictionary must contain name and value') cookie_kwargs = _cookie_attrs.copy() cookie_kwargs['rest'] = {} extra_args = set(dct) - set(cookie_kwargs) if extra_args: err = 'Unexpected keys in Cookie dictionary: {}' raise TypeError(err.format(sorted(extra_args))) cookie_kwargs.update(dct) for key, func in _bool_attrs: cookie_kwargs[key] = func(cookie_kwargs) return _Cookie(**cookie_kwargs)
Example #13
Source File: test_headers.py From http-observatory with Mozilla Public License 2.0 | 5 votes |
def test_session_cookie_no_secure_but_hsts(self): cookie = Cookie(name='SESSIONID', comment=None, comment_url=None, discard=False, domain='mozilla.com', domain_initial_dot=False, domain_specified='mozilla.com', expires=None, path='/', path_specified='/', port=443, port_specified=443, rfc2109=False, rest={'HttpOnly': True}, secure=False, version=1, value='bar') self.reqs['session'].cookies.set_cookie(cookie) self.reqs['responses']['https'].headers['Strict-Transport-Security'] = 'max-age=15768000' result = cookies(self.reqs) self.assertEquals('cookies-session-without-secure-flag-but-protected-by-hsts', result['result']) self.assertFalse(result['pass']) self.assertFalse(result['sameSite'])
Example #14
Source File: config.py From DataSpider with GNU General Public License v3.0 | 5 votes |
def _init_cookies(cookie_jar: CookieJar, firefox_cookies_path: str): """ Initialize cookies from firefox :param cookie_jar: :param firefox_cookies_path: :return: """ if firefox_cookies_path is None: firefox_cookies_path = __COOKIES_PATH con = sqlite3.connect(firefox_cookies_path) cur = con.cursor() # noinspection SqlResolve cur.execute("SELECT host, path, isSecure, expiry, name, value FROM moz_cookies") for item in cur.fetchall(): c = Cookie( 0, item[4], item[5], None, False, item[0], item[0].startswith('.'), item[0].startswith('.'), item[1], False, item[2], item[3], item[3] == "", None, None, {} ) cookie_jar.set_cookie(c) return cookie_jar
Example #15
Source File: common.py From iqiyi-parser with MIT License | 5 votes |
def setCookie(self, name, value, domain, path): self.cookiejar.set_cookie(Cookie(0, name, value, None, False, domain, True, False, path, True, False, None, False, None, None, None))
Example #16
Source File: common.py From iqiyi-parser with MIT License | 5 votes |
def loadCookie(self, cookie_str): self.cookie_str = cookie_str self.headers['Cookie'] = cookie_str self.initOpener()
Example #17
Source File: test_cookie.py From webium with Apache License 2.0 | 5 votes |
def test_convert_cookie_to_dict(self): cook1 = {'name': 'cook1', 'value': 'value1', 'path': '/', 'secure': False, 'expiry': 1453912471} cookie = Cookie( 0, cook1['name'], cook1['value'], None, False, '.github.com', True, True, cook1['path'], True, cook1['secure'], cook1['expiry'], False, None, None, None, False ) dict1 = convert_cookie_to_dict(cookie) eq_(cook1, dict1)
Example #18
Source File: SwaClient.py From southwest-autocheckin with Apache License 2.0 | 5 votes |
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
Example #19
Source File: api.py From musicbox with MIT License | 5 votes |
def _raw_request(self, method, endpoint, data=None): if method == "GET": resp = self.session.get( endpoint, params=data, headers=self.header, timeout=DEFAULT_TIMEOUT ) elif method == "POST": resp = self.session.post( endpoint, data=data, headers=self.header, timeout=DEFAULT_TIMEOUT ) return resp # 生成Cookie对象
Example #20
Source File: browsercookie.py From transistor with MIT License | 5 votes |
def create_cookie(host, path, secure, expires, name, value): """Shortcut function to create a cookie """ return cookielib.Cookie(0, name, value, None, False, host, host.startswith('.'), host.startswith('.'), path, True, secure, expires, False, None, None, {})
Example #21
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 #22
Source File: obs_operator.py From openSUSE-release-tools with GNU General Public License v2.0 | 5 votes |
def cookiejar_create(self, cookiejar_file, session): cookie_jar = LWPCookieJar(cookiejar_file.name) cookie_jar.set_cookie(Cookie(0, self.COOKIE_NAME, session, None, False, '', False, True, '/', True, True, None, None, None, None, {})) cookie_jar.save() cookiejar_file.flush()
Example #23
Source File: obs_operator.py From openSUSE-release-tools with GNU General Public License v2.0 | 5 votes |
def session_get(self): if self.session: return self.session else: cookie = self.headers.get('Cookie') if cookie: cookie = SimpleCookie(cookie) if self.COOKIE_NAME in cookie: return cookie[self.COOKIE_NAME].value return None
Example #24
Source File: test_helpers.py From codechef-cli with GNU General Public License v3.0 | 5 votes |
def test_init_session_cookie(self): """Should return cookiejar.Cookie instance with name and value as provided""" cookie = init_session_cookie("u", "u") self.assertIsInstance(cookie, Cookie) self.assertEqual(cookie.name, "u") self.assertEqual(cookie.value, "u")
Example #25
Source File: helpers.py From codechef-cli with GNU General Public License v3.0 | 5 votes |
def init_session_cookie(name, value, **kwargs): return Cookie(version=0, name=name, value=value, port=None, port_specified=False, domain='www.codechef.com', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=False, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)
Example #26
Source File: util.py From gallery-dl with GNU General Public License v2.0 | 4 votes |
def dump_response(response, fp, *, headers=False, content=True, hide_auth=True): """Write the contents of 'response' into a file-like object""" if headers: request = response.request req_headers = request.headers.copy() res_headers = response.headers.copy() outfmt = """\ {request.method} {request.url} Status: {response.status_code} {response.reason} Request Headers --------------- {request_headers} Response Headers ---------------- {response_headers} """ if hide_auth: authorization = req_headers.get("Authorization") if authorization: atype, sep, _ = authorization.partition(" ") req_headers["Authorization"] = atype + " ***" if sep else "***" cookie = req_headers.get("Cookie") if cookie: req_headers["Cookie"] = ";".join( c.partition("=")[0] + "=***" for c in cookie.split(";") ) set_cookie = res_headers.get("Set-Cookie") if set_cookie: res_headers["Set-Cookie"] = re.sub( r"(^|, )([^ =]+)=[^,;]*", r"\1\2=***", set_cookie, ) fp.write(outfmt.format( request=request, response=response, request_headers="\n".join( name + ": " + value for name, value in req_headers.items() ), response_headers="\n".join( name + ": " + value for name, value in res_headers.items() ), ).encode()) if content: if headers: fp.write(b"\nContent\n-------\n") fp.write(response.content)
Example #27
Source File: httptools.py From addon with GNU General Public License v3.0 | 4 votes |
def set_cookies(dict_cookie, clear=True, alfa_s=False): """ Guarda una cookie especifica en cookies.dat @param dict_cookie: diccionario de donde se obtienen los parametros de la cookie El dict debe contener: name: nombre de la cookie value: su valor/contenido domain: dominio al que apunta la cookie opcional: expires: tiempo de vida en segundos, si no se usa agregara 1 dia (86400s) @type dict_cookie: dict @param clear: True = borra las cookies del dominio, antes de agregar la nueva (necesario para cloudproxy, cp) False = desactivado por defecto, solo actualiza la cookie @type clear: bool """ #Se le dara a la cookie un dia de vida por defecto expires_plus = dict_cookie.get('expires', 86400) ts = int(time.time()) expires = ts + expires_plus name = dict_cookie.get('name', '') value = dict_cookie.get('value', '') domain = dict_cookie.get('domain', '') #Borramos las cookies ya existentes en dicho dominio (cp) if clear: try: cj.clear(domain) except: pass ck = cookielib.Cookie(version=0, name=name, value=value, port=None, port_specified=False, domain=domain, domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=expires, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False) cj.set_cookie(ck) save_cookies()
Example #28
Source File: jsoncookie.py From ITWSV with MIT License | 4 votes |
def cookiejar(self, domain): """Returns a cookielib.CookieJar object containing cookies matching the given domain.""" cj = CookieJar() if not domain: return cj # Domain comes from a urlparse().netloc so we must take care of optional port number search_ip = IP_REGEX.match(domain) if search_ip: # IPv4 (ex: '127.0.0.1') or IPv6 (ex: '[::1]') address. # We must append the '.local' suffix pour IPv6 addresses. domain = search_ip.group("ip") if domain.startswith("[") and not domain.endswith(".local"): domain += ".local" matching_domains = [domain] else: domain = domain.split(":")[0] # For hostnames on local network we must add a 'local' tld (needed by cookielib) if '.' not in domain: domain += ".local" domain_key = domain if domain[0] == '.' else '.' + domain exploded = domain_key.split(".") parent_domains = [".%s" % (".".join(exploded[x:])) for x in range(1, len(exploded) - 1)] matching_domains = [d for d in parent_domains if d in self.cookiedict] if not matching_domains: return cj for d in matching_domains: for path in self.cookiedict[d]: for cookie_name, cookie_attrs in self.cookiedict[d][path].items(): ck = Cookie( version=cookie_attrs["version"], name=cookie_name, value=cookie_attrs["value"], port=None, port_specified=False, domain=d, domain_specified=True, domain_initial_dot=False, path=path, path_specified=True, secure=cookie_attrs["secure"], expires=cookie_attrs["expires"], discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False ) if cookie_attrs["port"]: ck.port = cookie_attrs["port"] ck.port_specified = True cj.set_cookie(ck) return cj
Example #29
Source File: test_headers.py From http-observatory with Mozilla Public License 2.0 | 4 votes |
def test_session_no_secure(self): cookie = Cookie(name='SESSIONID', comment=None, comment_url=None, discard=False, domain='mozilla.com', domain_initial_dot=False, domain_specified='mozilla.com', expires=None, path='/', path_specified='/', port=443, port_specified=443, rfc2109=False, rest={'HttpOnly': True}, secure=False, version=1, value='bar') self.reqs['session'].cookies.set_cookie(cookie) result = cookies(self.reqs) self.assertEquals('cookies-session-without-secure-flag', result['result']) self.assertFalse(result['pass']) self.assertFalse(result['sameSite']) # https://github.com/mozilla/http-observatory/issues/97 cookie = Cookie(name='SESSIONID', comment=None, comment_url=None, discard=False, domain='mozilla.com', domain_initial_dot=False, domain_specified='mozilla.com', expires=None, path='/', path_specified='/', port=443, port_specified=443, rfc2109=False, rest={}, secure=False, version=1, value='bar') self.reqs['session'].cookies.set_cookie(cookie) result = cookies(self.reqs) self.assertEquals('cookies-session-without-secure-flag', result['result']) self.assertFalse(result['pass']) self.assertFalse(result['sameSite'])