Cookies are used for server implementation sessions, user login and related functions for state management. To install the Cookie,http server on the user's browser, add an HTTP header that resembles the following to the HTTP response:
The code is as follows:
set-cookie:session=8345234;expires=sun,15-nov-2013 15:00:00 gmt;path=/;domain=baidu.com
Expires is the life cycle of a cookie, path is the valid path to the cookie, and domain is the valid field of the cookie.
The path "path" is used to set the topmost directory where a cookie can be read. Setting the path of the cookie to the top-most directory of your page allows all pages in that directory to have access to the cookie.
Method: Add path=/to your cookie; If you only want the cookies to be available in the "Food" directory, you can join Path=/food.
Domain: Some sites have many small domain names, such as Baidu may also be in the "news.baidu.com" "zhidao.baidu.com" and "v.baidu.com" domain name has a page. If you want to allow all machines under "baidu.com" to read the cookie, you must include "domain=.baidu.com" in the cookie.
The user's browser stores the cookie until it expires, and the browser sends an HTTP request header similar to the following to the server that conforms to path and domain:
cookie:session=8345234.
For example, when landing www.baidu.com, the Baidu server sends back the HTTP response header in the cookie:
The code is as follows:
set-cookie:h_ps_pssid=4681_4567_1452_9876_4759; path=/; Domain=.baidu.com
set-cookie:bdsvrtm=74; path=/
HTTP request header for browser:
The code is as follows:
cookie:baiduid=0fd996sdfg12********107b9c227f4c:fg=1; Locale=zh; bdshare_firstime=1384567418140; nbid=d830dd2345hh2818a9f4134e5a2d778d3b:fg=1; h_ps_lc=4_shadu2014; Bd_ck_sam=1; h_ps_pssid=4681_4567_1452_9876_4759
When the browser sends the cookie back to the HTTP server, it uses the encoded form of the Key=value string and does not return optional properties such as Expires,path and domain.
The cookie string is usually located in the HTTP_COOKIE environment variable and can be read as follows:
The code is as follows:
Import OS
Print "content-type:text/plain\n"
If "Http_cookie" in Os.environ:
Print os.environ["Http_cookie"]
Else
Print "Http_cookie not set!"
The cookie module in Python (http.cookies in Python3) provides a special dictionary-like object Simplecookie, which stores and manages a collection of cookie values called morsel.
Each morsel has name,value and optional properties (expires,path,domain,comment,max-age,secure,version,httponly).
Simplecookie can use the output () method to create a cookie data export in the form of an HTTP header, using the Js_output () method to generate a string that contains JavaScript code.
Generate Cookies with Http_cookie:
The code is as follows:
Cookie=cookie.simplecookie (os.environ[' Http_cookie ')
Print Cookie.output ()
Set Cookie:
The code is as follows:
Import Cookie
Import datetime
Import Random
Expiration = Datetime.datetime.now () + Datetime.timedelta (days=30)
Cookie = Cookie.simplecookie ()
Cookie["session"] = Random.randint (1,1000000000)
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:
The code is as follows:
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 07:42:47 PST; path=/