For details about the usage of the Cookie module in Python, pythoncookie

Source: Internet
Author: User

For details about the usage of the Cookie module in Python, pythoncookie

Recently, GAE is used to develop its own blog program. Although GAE APIs do not explicitly provide Cookie operations, its existing architecture gives us sufficient freedom to operate cookies.

Cookie module, as its name implies, is a module used to operate cookies. Cookie is a small cake, which is used by the Server to maintain session with the Client. The Http protocol itself is stateless, that is, the two requests sent by the same client are not directly related to the Web server. In this case, some people may ask, since Http is stateless, why can some webpages be accessed only after the user name and password have been entered for verification? This is because: For authenticated users, the Server secretly adds cookies to the data sent to the Client. Generally, the Cookie stores a unique ID that identifies the Client, in the next request to the Server, the Client sends the ID to the Server in the form of a Cookie. The Server extracts the ID from the returned Cookie and binds it with the corresponding user, to implement identity authentication. To put it bluntly, a Cookie is a string that is transmitted between the server and the client (the Cookie used to access google.com through the FireBug plug-in of FireFox ). Go back to our topic: Python standard module-Cookie.

(Cookie information in the Http request header)

(Cookie information in the Http response)
The coke module defines four types of direct Cookie operations: BaseCookie, SimpleCookie, SerialCookie, and SmartCookie. Among them, BaseCookie is a base class that defines the public part of the Cookie operation. The other three classes are inherited from BaseCookie. The areas between them are different only in the way of serializing data. The following describes how to use these classes.

BaseCookie base class: BaseCookies act like dict and can be operated in the form of key/value pairs. However, kye must be a string and value is a Morsel object (we will talk about Morsel below ). BaseCookies define public specifications for encoding/decoding, input/output operations:

BaseCookie. value_encode (val): serialize/deserialize data. All these methods return strings for transmission over Http.

BaseCookie. output (): Return string, which can be used as the Http Response hair to the client.

BaseCookie. js_output (): returns the string embedded in the js script. The Browser executes the script to obtain the cooke data.

BaseCookie. load (newdata): parses the string as Cookie data.

SimpleCookie, SerialCookie, and SmartCookie all inherit from BaseCookie and have consistent behavior. They respectively rewrite the value_decode and value_encode of BaseCookie and implement their own serialization/deserialization policies:

  • SimpleCookie uses str () internally to serialize data;
  • SerialCookie uses the pickle module to serialize deserialization data;
  • SmartCookie is relatively smart. For non-string data, use the pickle sequence/deserialization. Otherwise, the string is returned as is.

The following example describes how to use the Cookie module:
 

Import Cookie c = Cookie. simpleCookie () c ['name'] = 'darkbull 'C ['address'] = 'chinahangzhou 'c ['address'] ['path'] ='/'# path c [' address '] ['domain'] = 'appspot. com '# domainc ['address'] ['expires'] = 'fir, 01---2010 20:00:00 gmt' # expiration time print c. output () print c. js_output () # output result, as compared to # Set-Cookie: address = ChinaHangZhou; Domain = appspot.com; expires = Fir, 01-Oct-2010 20:00:00 GMT; Path =/# Set-Cookie: name = DarkBull # output as a script # <script type = "text/javascript"> # document. cookie = "address = ChinaHangZhou; Domain = appspot.com; expires = Fir, 01-Oct-2010 20:00:00 GMT; Path = /"; # </script> # <script type = "text/javascript"> # document. cookie = "name = DarkBull"; # </script>

Morsel class: class used to abstract the attributes of each data item in a Cookie. These attributes include: expires, path, comment, domain, max-age, secure, version, and so on (see the underlined part ). If you have played web, you should be familiar with this. You can find their specific definitions in RCF2109.

Morsel. key, Morsel. value: key/value of the Cookie data item (value can be binary data );

Morsel. coded_value: String obtained after data encoding. Http is a text-based protocol. The Server cannot directly send binary data to the Client. It can only be sent to the Client after being serialized as a string;

Morsel. set (key, value, coded_value): set the key, value, and coded_value of the Cookie data item;

Morsel. isReversvedKey (key): If the key is expires, path, comment, domain, max-age, secure, version, or httponly, True is returned; otherwise, False is returned;

Morsel. output (): return type such as "Set-Cookie :..." Indicates a Cookie data item;

Morsel. js_output (): returns the script string of the Cookie data item;

Morsel. OutputString (): returns the string representation of Morsel;

Morsel usage example:
 

Import Cookie m = Cookie. morsel () m. set ('name', 'darkbull ', 'darkbull') m ['expires'] = 'fir, 01-Oct-2010 20:00:00 gmt' m ['domain'] = 'appspot. com 'print m. output () # result # Set-Cookie: name = DarkBull; Domain = appspot.com; expires = Fir, 01-Oct-2010 20:00:00

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.