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 that anyone who has ever played with the Web knows. It is the information used by the server to maintain a session with the client.
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, someone will ask, since HTTP is stateless
,
Why are some webpages accessible only after the user name and password have been entered for verification? This is because the server secretly adds
Add
A cookie stores a unique ID that identifies the client. In the next request to the server, the client will combine the ID in the form of a cookie.
To the server, the server extracts the ID from the returned cookie and binds it with the corresponding user to implement 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: cookie, the python standard module.
(Cookie information in the HTTP request header)
(Cookie information in the HTTP response)
The coke module defines four classes for direct cookie operations: basecookie, simplecookie, serialcookie,
Smartcookie. The basecookie is a base class that defines the public part of the cookie operation. The other three classes are inherited from the basecookie.
Do not just serialize data in different ways. 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, but Kye must be a string and ValueMorsel object
(Morsel ). Basecookies define public specifications for encoding/decoding, input/output operations:
Basecookie. value_decode (VAL)
Basecookie. value_encode (VAL ):
Serialize or deserialize data. All these methods return strings for transmission over HTTP.
Basecookie. Output ():
Returns a string that can be used as an HTTP Response hair to the client.
Basecookie. js_output ()
Return the string embedded in the JS script. the browser can execute the script to obtain the coke data.
Basecookie. Load (newdata): parses the string as cookie data.
Simplecookie, serialcookie, and smartcookie all inherit from basecookie and have consistent behavior.
The value_decode and value_encode of basecookie are overwritten to implement their own serialization/deserialization policies. Specifically:
- 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' # domain
C ['address'] ['expires'] = 'fir, 01---2010 20:00:00 gmt' # expiration time
Print C. Output ()
Print C. js_output ()
# Output result, comparison
# 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
:
This class is used to abstract the attributes of each data item in a cookie. These attributes include: expires, path, comment, domain,
Max-age, secure,
Version ). If you have ever 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 (): a string of the return type such as "Set-COOKIE:...", indicating 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---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 GMT
For more details about the Cookie module, refer to the python manual.