Detailed description of the cookie module used in Python

Source: Internet
Author: User
Tags parse string
Recently developed its own blogging program with Gae. Although Gae's API does not provide an explicit way to manipulate cookies, his existing architecture gives us enough freedom to manipulate cookies.

The cookie module, as its name implies, is the module used to manipulate cookies. Cookie This small cake, played by the Web people know, it is the server and the client to keep the conversation with the information slice. 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 will ask, since the HTTP is stateless, why some Web pages, only enter the user name and password to be authenticated before access? That's because for authenticated users, the server secretly adds a cookie,cookie to the data sent to the client, typically saving a unique id,client that identifies the client in the next request to the server, The ID is sent to server,server in the form of a cookie to extract the ID from the returned cookie and bind it to the appropriate user for authentication. To be blunt, a cookie is a string that passes between the server and the client (the Firebug plugin in Firefox is a way to view the cookie when accessing google.com). The farther away, the more we go back to our topic: Python standard module-cookie.

(is the cookie information in the HTTP request header)

(is the cookie information in the HTTP response)
The Cooke module defines 4 classes of direct-action cookies: Basecookie, Simplecookie, Serialcookie, Smartcookie. Where Basecookie is the base class that defines the public part of the manipulation cookie, and the other 3 classes inherit from Basecookie, the difference is only in the way that the data is serialized. Here is a brief explanation of the use of these classes.

Basecookie base class: Basecookies behaves much like dict, it can be manipulated in the form of a key/value pair, but Kye must be a string, and value is a morsel object (as described below morsel). Basecookies defines the common specification for encode/decode, input/output operations:

Basecookie.value_encode (val): Serialize/Deserialize the data. These methods all return strings for transmission over HTTP.

Basecookie.output (): Returns a string that can be used as an HTTP response to the hair to the client.

Basecookie.js_output (): Returns the string embedded in the JS script, and the browser can get Cooke data by executing the script.

Basecookie.load (NewData): Parse string as cookie data.

Simplecookie, Serialcookie, Smartcookie inherit from Basecookie, have consistent behavior, they each Basecookie Value_decode, Value_ Encode has rewritten and implemented its own serialization/deserialization strategy, where:

    • Simplecookie internal use of STR () to serialize the data;
    • Serialcookie the deserialization data through the Pickle module;
    • Smartcookie is relatively smart, for non-string data, use pickle sequence/deserialization, otherwise the string is returned as is.

The following example simply shows 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-oct-2010 20:00:00 GMT '  # Expiration Time Print C.output () print c.js_output () # Output results, with control # Set-cookie:address=chinahangzhou; domain=appspot.com; Expires=fir, 01-oct-2010 20:00:00 GMT; path=/# Set-cookie:name=darkbull # as script output #  

Morsel class: An abstract class used to represent the attributes of each item of data in a cookie. These properties include: Expires, path, comment, domain, max-age, secure, version, and so on (look at the underlined section). If you've ever played the Web, you shouldn't be unfamiliar with them, and you can find their specific definitions in RCF2109.

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

Morsel.coded_value: The string that is obtained after the data is encoded. HTTP protocol is a text-based protocol, the server can not directly send binary data to the client, only serialized into a string, can be sent to the client;

Morsel.set (key, Value, Coded_value): Sets the key, value, Coded_value of the cookie data item;

Morsel.isreversvedkey (Key): If key is expires, path, comment, domain, max-age, secure, version, and HttpOnly, returns True, otherwise returns false;

Morsel.output (): A string that returns a type such as "Set-cookie: ...", indicating a Cookie data item;

Morsel.js_output (): Returns the script string for the cookie data item;

Morsel.outputstring (): Returns the string representation of the morsel;

Morsel Use Example:

Import Cookie m = Cookie.morsel () m.set (' name ', ' darkbull ', ' darkbull ') m[' expires '] = ' Fir, 01-oct-2010 20:00:00 GMT ' m[' do Main '] = ' appspot.com ' Print m.output () # results # set-cookie:name=darkbull; domain=appspot.com; Expires=fir, 01-oct-2010 20:00:00
  • 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.