Cookies are used for session management on the server, and user logon and related functions perform status management. To install cookies on the user's browser, the HTTP server adds an HTTP header similar to the following content to the HTTP response: cookie is used for the server to implement session, and status management is performed when the user logs on and related functions. To install cookies in your browser, the HTTP server adds an HTTP header similar to the following to the HTTP response:
Set-Cookie: session = 8345234; expires = Sun, 15-Nov-2013 15:00:00 GMT; path =/; domain = baidu.com
Expires indicates the cookie lifecycle, path indicates the valid path of the cookie, and domain indicates the valid domain of the cookie.
The path "path" is used to set the top-level directory for reading a cookie. set the cookie path to the top-level directory of your webpage so that all webpages under this directory can access this cookie.
Method: Add path =/to your cookie. if you only want the webpage in the "food" directory to use this cookie, add path =/food.
Domain: some websites have many small domain names. for example, Baidu may still have webpages under "news.baidu.com" "zhidao.baidu.com" and "v.baidu.com. if you want all machines under "baidu.com" to read the cookie, you must add "domain = .baidu.com" to the cookie ".
The user's browser will store the Cookie until it expires, and the browser will send an HTTP request header similar to the following content to the server that complies with the path and domain:
Cookie: session = 8345234.
For example, when you log on to www.baidu.com, the cookie in the HTTP response header sent back by the Baidu server is:
Set-Cookie: H_PS_PSSID = 4682134567_1452_9876_4759; path =/; domain = .baidu.com
Set-Cookie: BDSVRTM = 74; path =/
HTTP request header of the browser:
Cookie: BAIDUID = 0FD996SDFG12 ******** rjb9c227f4c: FG = 1; locale = zh; bd1__firstime = 1384567418140; NBID = fingerprint: FG = 1; H_PS_LC = 4_shadu2014; BD_CK_SAM = 1; H_PS_PSSID = 4682134567_1452_9876_4759
When the browser sends the cookie back to the HTTP server, it uses the key = value string encoding format. no optional attributes such as expires, path, and domain are returned.
The cookie string is usually located in the HTTP_COOKIE environment variable and can be read as follows:
Import osPRint "Content-type: text/plain \ n"
If "HTTP_COOKIE" in OS. environ:
Print OS. environ ["HTTP_COOKIE"]
Else:
Print "HTTP_COOKIE not set! "
In Python, the Cookie module (http. cookies in python3) provides a dictionary-like special object SimpleCookie, which stores and manages a set of cookie values called Morsel.
Each Morsel has the name, value, and optional attributes (expires, path, domain, comment, max-age, secure, version, httponly ).
SimpleCookie can use the output () method to create a cookie data output in the form of an HTTP header, and use the js_output () method to generate a string containing javascript code.
Use HTTP_COOKIE to generate a cookie:
Cookie = Cookie. SimpleCookie (OS. environ ['http _ cookier'])
Print cookie. output ()
Set cookie:
Import Cookie
Import datetime
Import random
Expiration = datetime. datetime. now () + datetime. timedelta (days = 30)
Cookie = Cookie. SimpleCookie ()
Cookie ["session"] = random. randint (1,000000000)
Cookie ["session"] ["domain"] = ".baidu.com"
Cookie ["session"] ["path"] = "/"
Cookie ["session"] ["expires"] = expiration. strftime ("% a, % d-% B-% Y % H: % M: % S PST ")
Print "Content-type: text/plain"
Print cookie. output ()
Print
Print "Cookie set with:" + cookie. output ()
Output:
Content-type: text/plain
Set-Cookie: session = 155209565; Domain = .jayconrod.com; expires = Mon, 03-Mar-2014 07:42:47 PST; Path =/Cookie set with: Set-Cookie: session = 155209565; domain = .jayconrod.com; expires = Mon, 03-Mar-2014
The above is the processing of cookies in Python (1) the content of the Cookie Library. For more information, see PHP Chinese network (www.php1.cn )!