Python Bottle framework to obtain the cookie-making tutorial, pythonbottle
In the past two days, the login function has been added to a project written using bottle + mongodb, but the stored cookie cannot be obtained. This document provides the code snippet that allows us to perform such operations on the cookie:
@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.