The previous article talked about web.py first page Hello Word, below we continue to drill down into web.py programming, to say web.py cookie settings.
Cookies believe that the person who has learned web programming is not unfamiliar, and it plays a very important role in web programming. User login, Verification code, session (based on cookie), language selector, identification and so on will have a cookie figure. So how do you set a cookie in web.py?
In fact, setting cookies in web.py is very simple, and web.py has already thought of this for us, providing a very simple and useful function:
Setcookie (name, Value, expires= "", Domain=none, Secure=false)
Detailed parameters:
Name (string)-the name of the cookie, which is saved by the browser and sent to the server.
The value of value (String)-cookie corresponds to the name of the cookie.
Expires (int)-The expiration time of the cookie, which is an optional parameter that determines how long the cookie is valid. The unit is in seconds. It must be an integer, not a string. Optional parameter, which is permanently valid by default if the parameter is not written.
Domain (string)-the valid domain of the cookie-the cookie is valid within that domain. In general, to be available within a site, this parameter value is written to the site's domain (for example,. webpy.org) instead of the host name of the station (such as wiki.webpy.org), optional parameters
Secure (BOOL)-if true, requires that the cookie be transmitted only over HTTPS. Optional parameters
For example:
#设置website的值为www. pythontab.com, valid for 60 Seconds Web.setcookie ("website", "www.pythontab.com", 60)
Example
Set the cookie with Web.setcookie () as follows:
Class Cookieset: def GET (self): i = web.input (age= ') web.setcookie (' age ', I.age, 3600) return "age Set in your cookie "
Calling the above class with get will set a cookie named age with a default value of 25 (in fact, the default value of 25 is given to I.age in Web.input, which indirectly gives the cookie, rather than directly to the cookie in the Setcookie function). This cookie expires in an hour (i.e., 3,600 seconds).
The third parameter of Web.setcookie ()-"Expires" is an optional parameter that is used to set the time at which the cookie expires. If it is negative, the cookie will expire immediately. A positive number indicates how long the cookie is valid, in seconds. If this parameter is blank, the cookie will never expire.
Access to Cookies
Overview
There are many ways to get the value of a cookie, and the difference is how it is handled when the cookie is not found.
Method 1 (returns None if the cookie is not found):
Get by the Get method
#通过设置的cookie的名字获取cookie, such as website#web.cookies (). Get ("website") web.cookies (). Get (CookieName)
Method 2 (throws a Attributeerror exception if no cookie is found):
#先把cookie对象赋值给一个变量 and then get it by the name of the cookie # for example: Foo.websitefoo = Web.cookies () foo.cookiename
Method 3 (If you cannot find a cookie, you can set a default value to avoid throwing an exception):
#该方法最大的特点就是可以设置cookie的默认值foo = Web.cookies (cookiename=defaultvalue) #如果不存在该cookieName, The default cookiefoo.cookiename for the setting is returned
If you want to confirm that the cookie value exists,
You can do this:
Class Cookieget: def GET (self): try: return "Your website name is:" + web.cookies (). website except: #抛出异常处理 return "Cookie does not exist."
Or
Class Cookieget: def get (self): #先进行赋值 website = web.cookies (). GET (' website ') If-age: return " Your website name is:%s "% website else: return" Cookie does not exist. "