In Python's bottle framework, get a tutorial on making cookies

Source: Internet
Author: User
Tags in python

in Python's bottle framework, get a tutorial on making cookies

This article mainly introduces Python's bottle framework to get cookie-making tutorial, mainly for other paths rather than the current page of cookies, need friends can refer to the

These two days for a project written with Bottle+mongodb plus login function, but how can not get the saved cookies, the document gives us this operation cookies code fragment:

?

1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 @route ('/login ') def login (): username = Request. Forms. Get (' username ') password = Request. Forms. Get (' password ') if Check_user_credentials (username, password): Response. Set_cookie ("Account", username, secret= ' Some-secret-key ') Return "Welcome%s!   You are are now logged the% Username Else:return "Login failed." @route ('/restricted ') def restricted_area (): username = Request. Get_cookie ("account", secret= ' Some-secret-key ') if use Rname:return "Hello%s.welcome back."% username

Although there is no documentation but there is a way to manipulate cookies:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17 From bottle import request, Response @route ('/login ', method= "POST") def login (): User = Request. post[' user '] passwd = Request. post[' passwd '] if Check_user_right (USER,PASSWD): Response. cookies[' account ' = user Else:pass @route ('/admin ') def admin (): User = Request. cookies[' user ' if User:pass

But no matter which way I operate I can not get cookies, why not. Baffled. But one of my handling articles was a click-through rate that reminded me of the code as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 @route ('/archrives/:aid#d+# ') def article_show (aid): db = Dbconn. CONNDB () artid = Int (aid) # get client IP Remoteip = request.environ.get (' remote_addr ') Artcookie = remoteip+ ' IP ' +aid print re Quest. Cookies.keys () # Determines whether the cookie exists if artcookie in request. Cookies.keys (): # exists to update the active time response. Cookies[artcookie] = True response. cookies[artcookie][' max-age '] = + Else: # Update Article view times db.posts.update ({"id": ArtID}, {"$inc": {"views": 1}}) # and set Cooki E Response. Cookies[artcookie] = True response. cookies[artcookie][' max-age '] = + template[' posts '] = Getartlist ({"id": ArtID}) Template.update (SetTempVar ()) return Template (' article.html ', template)

This is a normal way to get cookies, and the code doesn't make any difference. The only difference is that user authentication is a jump to the page. So I help a bit:

?

1 2 From bottle import response Help (Response.set_cookie)

The result of Help has two parameters, one is path, and domain:

?

1 2 3 :p Aram Domain:the Domain is allowed to read the cookie. (default:current domain):p Aram path:limits The cookie to a given path (default:current path)

Obviously bottle cookies are only readable by default in the current path, so the code to read cookies to another page should be changed as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 From bottle import request, Response @route ('/login ', method= "POST") def login (): User = Request. post[' user '] passwd = Request. post[' passwd '] if Check_user_right (USER,PASSWD): Response. cookies[' account ' = user response. cookies[' account ' [' path '] = '/admin ' else:pass @route ('/admin ') def admin (): User = Request. cookies[' user ']

So that we can access the cookie we set under another path.

Related Article

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.