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:
@route ('/login ')
def login ():
username = Request. Forms. Get (' username ')
password = Request. Forms. Get (' PA ssWOrd ')
if check_user_credentials (username, password):
response. Set_cookie ("Account", username, secret= ') Some-secret-key ') return
"Welcome%s! You are 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 there is no documentation but there is a way to manipulate cookies:
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:
@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 request. 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:
# does not exist update Article view Count
db.posts.update ({"id": ArtID}, {"$inc": {" Views ": 1}})
# and set the cookie
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:
From bottle import response help
(Response.set_cookie)
The result of Help has two parameters, one is path, and domain:
: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:
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.