MCPcopy Index your code
hub / github.com/fluentpython/example-code-2e / set_cookie

Method set_cookie

21-async/mojifinder/bottle.py:1595–1649  ·  view source on GitHub ↗

Create a new cookie or replace an old one. If the `secret` parameter is set, create a `Signed Cookie` (described below). :param name: the name of the cookie. :param value: the value of the cookie. :param secret: a signature key required for signed co

(self, name, value, secret=None, **options)

Source from the content-addressed store, hash-verified

1593 return default
1594
1595 def set_cookie(self, name, value, secret=None, **options):
1596 ''' Create a new cookie or replace an old one. If the `secret` parameter is
1597 set, create a `Signed Cookie` (described below).
1598
1599 :param name: the name of the cookie.
1600 :param value: the value of the cookie.
1601 :param secret: a signature key required for signed cookies.
1602
1603 Additionally, this method accepts all RFC 2109 attributes that are
1604 supported by :class:`cookie.Morsel`, including:
1605
1606 :param max_age: maximum age in seconds. (default: None)
1607 :param expires: a datetime object or UNIX timestamp. (default: None)
1608 :param domain: the domain that is allowed to read the cookie.
1609 (default: current domain)
1610 :param path: limits the cookie to a given path (default: current path)
1611 :param secure: limit the cookie to HTTPS connections (default: off).
1612 :param httponly: prevents client-side javascript to read this cookie
1613 (default: off, requires Python 2.6 or newer).
1614
1615 If neither `expires` nor `max_age` is set (default), the cookie will
1616 expire at the end of the browser session (as soon as the browser
1617 window is closed).
1618
1619 Signed cookies may store any pickle-able object and are
1620 cryptographically signed to prevent manipulation. Keep in mind that
1621 cookies are limited to 4kb in most browsers.
1622
1623 Warning: Signed cookies are not encrypted (the client can still see
1624 the content) and not copy-protected (the client can restore an old
1625 cookie). The main intention is to make pickling and unpickling
1626 save, not to store secret information at client side.
1627 '''
1628 if not self._cookies:
1629 self._cookies = SimpleCookie()
1630
1631 if secret:
1632 value = touni(cookie_encode((name, value), secret))
1633 elif not isinstance(value, basestring):
1634 raise TypeError('Secret key missing for non-string Cookie.')
1635
1636 if len(value) > 4096: raise ValueError('Cookie value to long.')
1637 self._cookies[name] = value
1638
1639 for key, value in options.items():
1640 if key == 'max_age':
1641 if isinstance(value, timedelta):
1642 value = value.seconds + value.days * 24 * 3600
1643 if key == 'expires':
1644 if isinstance(value, (datedate, datetime)):
1645 value = value.timetuple()
1646 elif isinstance(value, (int, float)):
1647 value = time.gmtime(value)
1648 value = time.strftime("%a, %d %b %Y %H:%M:%S GMT", value)
1649 self._cookies[name][key.replace('_', '-')] = value
1650
1651 def delete_cookie(self, key, **kwargs):
1652 ''' Delete a cookie. Be sure to use the same `domain` and `path`

Callers 1

delete_cookieMethod · 0.95

Calls 4

touniFunction · 0.85
cookie_encodeFunction · 0.85
itemsMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected