This article describes how to obtain the cookie in the Python Bottle framework. it mainly targets other paths rather than cookies on the current page, if you need a user, you can refer to the login function added to a project written in bottle + mongodb over the next two days, but you cannot get the saved cookie, this document provides the code snippet that allows us to perform such operations on cookies:
@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 now logged in." % username else : return "Login failed." @route('/restricted')def restricted_area (): username = request .get_cookie("account", secret= 'some-secret-key') if username: return "Hello %s.Welcome back." % username
Although not found in the document, there is also a way to operate the cookie:
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
However, no matter which method I use, I cannot obtain the cookie. why? I am puzzled. However, my code for handling the article's click rate reminds me that the code is as follows:
@ Route ('/archrives/: aid # \ d + #') def article_show (aid): db = dbconn. connDB () artid = int (aid) # obtain the client ip remoteip = request. environ. get ('remote _ ADDR ') artcookie = remoteip + 'IP' + aid print request. COOKIES. keys () # determine whether the cookie exists if artcookie in request. COOKIES. keys (): # if yes, update the effective time response. COOKIES [artcookie] = True response. COOKIES [artcookie] ['Max-age'] = 500 else: # update the number of Times articles are viewed db if they do not exist. posts. update ({"id": artid },{ "$ inc": {"views": 1 }}) # Set cookie response. COOKIES [artcookie] = True response. COOKIES [artcookie] ['Max-age'] = 500 TEMPLATE ['posts'] = getArtList ({"id": artid}) TEMPLATE. update (setTempVar () return template('article.html ', TEMPLATE)
Here we can get the cookie normally, and there is no difference in the code. The only difference is that user authentication is redirected to the page. so I helped:
from bottle import responsehelp(response.set_cookie)
In The help result, there are two parameters: path and domain:
:param domain: the domain that is allowed to read the cookie. (default: current domain) :param path: limits the cookie to a given path (default: current path)
Obviously, bottle cookies can only be read in the current path by default. therefore, to read cookies from other pages, our code must be changed to the following:
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']
In this way, we can access the configured cookie in another path.