开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

source-code-analysis/python3.8.1

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
python3.8.1
/
Doc
/
library
/
http.cookies.rst
python3.8.1
/
Doc
/
library
/
http.cookies.rst
http.cookies.rst 8.79 KB
一键复制 编辑 原始数据 按行查看 历史
zhangweibo 提交于 2021年11月16日 09:46 +08:00 . git init

:mod:`http.cookies` --- HTTP state management

.. module:: http.cookies
 :synopsis: Support for HTTP state management (cookies).

.. moduleauthor:: Timothy O'Malley <timo@alum.mit.edu>
.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>

Source code: :source:`Lib/http/cookies.py`


The :mod:`http.cookies` module defines classes for abstracting the concept of cookies, an HTTP state management mechanism. It supports both simple string-only cookies, and provides an abstraction for having any serializable data-type as cookie value.

The module formerly strictly applied the parsing rules described in the RFC 2068 specifications. It has since been discovered that MSIE 3.0x doesn't follow the character rules outlined in those specs and also many current day browsers and servers have relaxed parsing rules when comes to Cookie handling. As a result, the parsing rules used are a bit less strict.

The character set, :data:`string.ascii_letters`, :data:`string.digits` and !#$%&'*+-.^_`|~: denote the set of valid characters allowed by this module in Cookie name (as :attr:`~Morsel.key`).

.. versionchanged:: 3.3
 Allowed ':' as a valid Cookie name character.


Note

On encountering an invalid cookie, :exc:`CookieError` is raised, so if your cookie data comes from a browser you should always prepare for invalid data and catch :exc:`CookieError` on parsing.

.. exception:: CookieError

 Exception failing because of :rfc:`2109` invalidity: incorrect attributes,
 incorrect :mailheader:`Set-Cookie` header, etc.


This class is a dictionary-like object whose keys are strings and whose values are :class:`Morsel` instances. Note that upon setting a key to a value, the value is first converted to a :class:`Morsel` containing the key and the value.

If input is given, it is passed to the :meth:`load` method.

This class derives from :class:`BaseCookie` and overrides :meth:`value_decode` and :meth:`value_encode`. SimpleCookie supports strings as cookie values. When setting the value, SimpleCookie calls the builtin :func:`str()` to convert the value to a string. Values received from HTTP are kept as strings.

.. seealso::

 Module :mod:`http.cookiejar`
 HTTP cookie handling for web *clients*. The :mod:`http.cookiejar` and
 :mod:`http.cookies` modules do not depend on each other.

 :rfc:`2109` - HTTP State Management Mechanism
 This is the state management specification implemented by this module.


Cookie Objects

.. method:: BaseCookie.value_decode(val)

 Return a tuple ``(real_value, coded_value)`` from a string representation.
 ``real_value`` can be any type. This method does no decoding in
 :class:`BaseCookie` --- it exists so it can be overridden.


.. method:: BaseCookie.value_encode(val)

 Return a tuple ``(real_value, coded_value)``. *val* can be any type, but
 ``coded_value`` will always be converted to a string.
 This method does no encoding in :class:`BaseCookie` --- it exists so it can
 be overridden.

 In general, it should be the case that :meth:`value_encode` and
 :meth:`value_decode` are inverses on the range of *value_decode*.


.. method:: BaseCookie.output(attrs=None, header='Set-Cookie:', sep='\\r\\n')

 Return a string representation suitable to be sent as HTTP headers. *attrs* and
 *header* are sent to each :class:`Morsel`'s :meth:`output` method. *sep* is used
 to join the headers together, and is by default the combination ``'\r\n'``
 (CRLF).


.. method:: BaseCookie.js_output(attrs=None)

 Return an embeddable JavaScript snippet, which, if run on a browser which
 supports JavaScript, will act the same as if the HTTP headers was sent.

 The meaning for *attrs* is the same as in :meth:`output`.


.. method:: BaseCookie.load(rawdata)

 If *rawdata* is a string, parse it as an ``HTTP_COOKIE`` and add the values
 found there as :class:`Morsel`\ s. If it is a dictionary, it is equivalent to::

 for k, v in rawdata.items():
 cookie[k] = v


Morsel Objects

Abstract a key/value pair, which has some RFC 2109 attributes, which are

  • expires
  • path
  • comment
  • domain
  • max-age
  • secure
  • version
  • httponly
  • samesite

The attribute :attr:`httponly` specifies that the cookie is only transferred in HTTP requests, and is not accessible through JavaScript. This is intended to mitigate some forms of cross-site scripting.

The attribute :attr:`samesite` specifies that the browser is not allowed to send the cookie along with cross-site requests. This helps to mitigate CSRF attacks. Valid values for this attribute are "Strict" and "Lax".

The keys are case-insensitive and their default value is ''.

.. versionchanged:: 3.5
 :meth:`~Morsel.__eq__` now takes :attr:`~Morsel.key` and :attr:`~Morsel.value`
 into account.

.. versionchanged:: 3.7
 Attributes :attr:`~Morsel.key`, :attr:`~Morsel.value` and
 :attr:`~Morsel.coded_value` are read-only. Use :meth:`~Morsel.set` for
 setting them.

.. versionchanged:: 3.8
 Added support for the :attr:`samesite` attribute.
.. attribute:: Morsel.value

 The value of the cookie.


.. attribute:: Morsel.coded_value

 The encoded value of the cookie --- this is what should be sent.


.. attribute:: Morsel.key

 The name of the cookie.


.. method:: Morsel.set(key, value, coded_value)

 Set the *key*, *value* and *coded_value* attributes.


.. method:: Morsel.isReservedKey(K)

 Whether *K* is a member of the set of keys of a :class:`Morsel`.


.. method:: Morsel.output(attrs=None, header='Set-Cookie:')

 Return a string representation of the Morsel, suitable to be sent as an HTTP
 header. By default, all the attributes are included, unless *attrs* is given, in
 which case it should be a list of attributes to use. *header* is by default
 ``"Set-Cookie:"``.


.. method:: Morsel.js_output(attrs=None)

 Return an embeddable JavaScript snippet, which, if run on a browser which
 supports JavaScript, will act the same as if the HTTP header was sent.

 The meaning for *attrs* is the same as in :meth:`output`.


.. method:: Morsel.OutputString(attrs=None)

 Return a string representing the Morsel, without any surrounding HTTP or
 JavaScript.

 The meaning for *attrs* is the same as in :meth:`output`.


.. method:: Morsel.update(values)

 Update the values in the Morsel dictionary with the values in the dictionary
 *values*. Raise an error if any of the keys in the *values* dict is not a
 valid :rfc:`2109` attribute.

 .. versionchanged:: 3.5
 an error is raised for invalid keys.


.. method:: Morsel.copy(value)

 Return a shallow copy of the Morsel object.

 .. versionchanged:: 3.5
 return a Morsel object instead of a dict.


.. method:: Morsel.setdefault(key, value=None)

 Raise an error if key is not a valid :rfc:`2109` attribute, otherwise
 behave the same as :meth:`dict.setdefault`.


Example

The following example demonstrates how to use the :mod:`http.cookies` module.

>>> from http import cookies
>>> C = cookies.SimpleCookie()
>>> C["fig"] = "newton"
>>> C["sugar"] = "wafer"
>>> print(C) # generate HTTP headers
Set-Cookie: fig=newton
Set-Cookie: sugar=wafer
>>> print(C.output()) # same thing
Set-Cookie: fig=newton
Set-Cookie: sugar=wafer
>>> C = cookies.SimpleCookie()
>>> C["rocky"] = "road"
>>> C["rocky"]["path"] = "/cookie"
>>> print(C.output(header="Cookie:"))
Cookie: rocky=road; Path=/cookie
>>> print(C.output(attrs=[], header="Cookie:"))
Cookie: rocky=road
>>> C = cookies.SimpleCookie()
>>> C.load("chips=ahoy; vienna=finger") # load from a string (HTTP header)
>>> print(C)
Set-Cookie: chips=ahoy
Set-Cookie: vienna=finger
>>> C = cookies.SimpleCookie()
>>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\012円;";')
>>> print(C)
Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=012円;"
>>> C = cookies.SimpleCookie()
>>> C["oreo"] = "doublestuff"
>>> C["oreo"]["path"] = "/"
>>> print(C)
Set-Cookie: oreo=doublestuff; Path=/
>>> C = cookies.SimpleCookie()
>>> C["twix"] = "none for you"
>>> C["twix"].value
'none for you'
>>> C = cookies.SimpleCookie()
>>> C["number"] = 7 # equivalent to C["number"] = str(7)
>>> C["string"] = "seven"
>>> C["number"].value
'7'
>>> C["string"].value
'seven'
>>> print(C)
Set-Cookie: number=7
Set-Cookie: string=seven
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/python_sourcecode/python3.8.1.git
git@gitee.com:python_sourcecode/python3.8.1.git
python_sourcecode
python3.8.1
python3.8.1
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /