Every line of 'python requests set cookie' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.
157 def set_cookies(self, cookies): 158 """ 159 Sets new cookies from a string 160 """ 161 c = SimpleCookie() 162 c.load(cookies) 163 for key, m in c.items(): 164 self.s.cookies.set(key, m.value)
359 def set_cookie(self, key, value, **kargs): 360 """ Sets a Cookie. Optional settings: expires, path, comment, domain, max-age, secure, version, httponly """ 361 self.COOKIES[key] = value 362 for k in kargs: 363 self.COOKIES[key][k] = kargs[k]
184 def set_cookie(response, name, value, expiry_seconds=None, secure=False): 185 """ 186 Set cookie wrapper that allows number of seconds to be given as the 187 expiry time, and ensures values are correctly encoded. 188 """ 189 if expiry_seconds is None: 190 expiry_seconds = 90 * 24 * 60 * 60 # Default to 90 days. 191 expires = datetime.strftime(datetime.utcnow() + 192 timedelta(seconds=expiry_seconds), 193 "%a, %d-%b-%Y %H:%M:%S GMT") 194 # Django doesn't seem to support unicode cookie keys correctly on 195 # Python 2. Work around by encoding it. See 196 # https://code.djangoproject.com/ticket/19802 197 try: 198 response.set_cookie(name, value, expires=expires, secure=secure) 199 except (KeyError, TypeError): 200 response.set_cookie(name.encode('utf-8'), value, expires=expires, 201 secure=secure)
45 def setCookie(self, key, val, domain): 46 ck = cookielib.Cookie(version=0, name=key, value=val, port=None, port_specified=False, domain=domain, 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) 47 self.__cookie.set_cookie(ck)
487 def set_cookie( 488 name: str, 489 value: str, 490 url: typing.Optional[str] = None, 491 domain: typing.Optional[str] = None, 492 path: typing.Optional[str] = None, 493 secure: typing.Optional[bool] = None, 494 http_only: typing.Optional[bool] = None, 495 same_site: typing.Optional[CookieSameSite] = None, 496 expires: typing.Optional[TimeSinceEpoch] = None, 497 ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,bool]: 498 ''' 499 Sets a cookie with the given cookie data; may overwrite equivalent cookies if they exist. 500 501 :param name: Cookie name. 502 :param value: Cookie value. 503 :param url: The request-URI to associate with the setting of the cookie. This value can affect the 504 default domain and path values of the created cookie. 505 :param domain: Cookie domain. 506 :param path: Cookie path. 507 :param secure: True if cookie is secure. 508 :param http_only: True if cookie is http-only. 509 :param same_site: Cookie SameSite type. 510 :param expires: Cookie expiration date, session cookie if not set 511 :returns: True if successfully set cookie. 512 ''' 513 params: T_JSON_DICT = { 514 'name': name, 515 'value': value, 516 } 517 if url is not None: 518 params['url'] = url 519 if domain is not None: 520 params['domain'] = domain 521 if path is not None: 522 params['path'] = path 523 if secure is not None: 524 params['secure'] = secure 525 if http_only is not None: 526 params['httpOnly'] = http_only 527 if same_site is not None: 528 params['sameSite'] = same_site.to_json() 529 if expires is not None: 530 params['expires'] = expires.to_json() 531 cmd_dict: T_JSON_DICT = { 532 'method': 'Network.setCookie', 533 'params': params, 534 } 535 json = yield cmd_dict 536 return bool(json['success'])
1449 def set_cookie_if_ok(self, cookie, request): 1450 """Set a cookie if policy says it's OK to do so. 1451 1452 cookie: ClientCookie.Cookie instance 1453 request: see extract_cookies.__doc__ for the required interface 1454 1455 """ 1456 self._policy._now = self._now = int(time.time()) 1457 1458 if self._policy.set_ok(cookie, request): 1459 self.set_cookie(cookie)
104 def set_cookie(self, name, secret): 105 max_age = (self.expires - datetime.datetime.now()).seconds 106 response.set_cookie(name, self.id, path='/', secret=secret, 107 max_age=max_age)
123 def set_cookie(self, name, value, expire=3600, **kwargs): 124 if value > 4095: 125 raise RuntimeWarning() 126 result = f"{name}={value}; expires={timestamp_toCookie(time() + expire)};" 127 for k, v in kwargs.items(): 128 if k and v: 129 result += f' {k.replace("_", "-")}={v};' 130 elif k: 131 result += f" {k};" 132 self.header["Set-Cookie"] = result
370 def set_cookie(self, cookie_string): 371 """Request to set a cookie. 372 373 Note that it is NOT necessary to call this method under ordinary 374 circumstances: cookie handling is normally entirely automatic. The 375 intended use case is rather to simulate the setting of a cookie by 376 client script in a web page (e.g. JavaScript). In that case, use of 377 this method is necessary because mechanize currently does not support 378 JavaScript, VBScript, etc. 379 380 The cookie is added in the same way as if it had arrived with the 381 current response, as a result of the current request. This means that, 382 for example, if it is not appropriate to set the cookie based on the 383 current request, no cookie will be set. 384 385 The cookie will be returned automatically with subsequent responses 386 made by the Browser instance whenever that's appropriate. 387 388 cookie_string should be a valid value of the Set-Cookie header. 389 390 For example: 391 392 browser.set_cookie( 393 "sid=abcdef; expires=Wednesday, 09-Nov-06 23:12:40 GMT") 394 395 Currently, this method does not allow for adding RFC 2986 cookies. 396 This limitation will be lifted if anybody requests it. 397 398 """ 399 if self._response is None: 400 raise BrowserStateError("not viewing any document") 401 if self.request.get_type() not in ["http", "https"]: 402 raise BrowserStateError("can't set cookie for non-HTTP/HTTPS " 403 "transactions") 404 cookiejar = self._ua_handlers["_cookies"].cookiejar 405 response = self.response() # copy 406 headers = response.info() 407 headers["Set-cookie"] = cookie_string 408 cookiejar.extract_cookies(response, self.request)
779 def set_response_cookie(path=None, path_header=None, name='session_id', 780 timeout=60, domain=None, secure=False): 781 """Set a response cookie for the client. 782 783 path 784 the 'path' value to stick in the response cookie metadata. 785 786 path_header 787 if 'path' is None (the default), then the response 788 cookie 'path' will be pulled from request.headers[path_header]. 789 790 name 791 the name of the cookie. 792 793 timeout 794 the expiration timeout for the cookie. If 0 or other boolean 795 False, no 'expires' param will be set, and the cookie will be a 796 "session cookie" which expires when the browser is closed. 797 798 domain 799 the cookie domain. 800 801 secure 802 if False (the default) the cookie 'secure' value will not 803 be set. If True, the cookie 'secure' value will be set (to 1). 804 805 """ 806 # Set response cookie 807 cookie = cherrypy.serving.response.cookie 808 cookie[name] = cherrypy.serving.session.id 809 cookie[name]['path'] = (path or cherrypy.serving.request.headers.get(path_header) 810 or '/') 811 812 # We'd like to use the "max-age" param as indicated in 813 # http://www.faqs.org/rfcs/rfc2109.html but IE doesn't 814 # save it to disk and the session is lost if people close 815 # the browser. So we have to use the old "expires" ... sigh ... 816 ## cookie[name]['max-age'] = timeout * 60 817 if timeout: 818 e = time.time() + (timeout * 60) 819 cookie[name]['expires'] = httputil.HTTPDate(e) 820 if domain is not None: 821 cookie[name]['domain'] = domain 822 if secure: 823 cookie[name]['secure'] = 1