Python use cookie Library to save cookies in detail _python

Source: Internet
Author: User

Cookies are used for server implementation sessions, user login and related functions for state management. To install the Cookie,http server in the user's browser, add an HTTP header that resembles the following in the HTTP response:

Copy Code code as follows:

set-cookie:session=8345234;expires=sun,15-nov-2013 15:00:00 gmt;path=/;domain=baidu.com

Expires is the lifetime of a cookie, path is a valid path for cookies, and domain is a valid domain for cookies.

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 Web page allows the cookie to be accessible to all pages in that directory.

Method: Add path=/to your cookie; If you only want the page in the "Food" directory to use the cookie, you can join Path=/food.

Domain: Some sites have many small domain names, such as Baidu may also be "news.baidu.com" "zhidao.baidu.com" and "v.baidu.com" under the Domain Name page. If you want all machines under "baidu.com" to read the cookie, you must include "domain=.baidu.com" in the cookie.

The user's browser stores cookies until they expire, and the browser sends an HTTP request header similar to the following for servers that match path and domain:

cookie:session=8345234.

For example, when landing www.baidu.com, the HTTP response header sent back by the Baidu server is a cookie:

Copy Code code 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 the browser:

Copy Code code 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 encoding form of the Key=value string and does not return optional attributes such as Expires,path and domain.

The cookie string is usually located in the HTTP_COOKIE environment variable and can be read as follows:

Copy Code code 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 dictionary-like special object Simplecookie that stores and manages a collection of cookie values called morsel.

Each morsel has 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, using the Js_output () method to generate a string that contains JavaScript code.

To generate cookies with Http_cookie:

Copy Code code as follows:

Cookie=cookie.simplecookie (os.environ[' Http_cookie ')
Print Cookie.output ()

Set Cookie:

Copy Code code 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:

Copy Code code 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=/

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.