黑松山资源网 Design By www.paidiu.com
这两天为用bottle+mongodb写的一个项目加上登录功能,无奈怎么都获取不到保存的cookie,文档给出让我们这样操作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
虽然文档上没有但是还有一种操作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
但是无论我用哪种方式操作我都无法获取cookie,为什么呢.百思不得其解.但是我的一个处理文章点击率的提醒了我,代码如下:
@route('/archrives/:aid#\d+#') def article_show(aid): db = dbconn.ConnDB() artid = int(aid) # 获取客户端ip remoteip = request.environ.get('REMOTE_ADDR') artcookie = remoteip+'ip'+aid print request.COOKIES.keys() # 判断cookie是否存在 if artcookie in request.COOKIES.keys(): # 存在则更新有效时间 response.COOKIES[artcookie] = True response.COOKIES[artcookie]['max-age'] = 500 else: # 不存在则更新文章查看次数 db.posts.update({"id":artid}, {"$inc":{"views":1}}) # 并设置cookie response.COOKIES[artcookie] = True response.COOKIES[artcookie]['max-age'] = 500 TEMPLATE['posts'] = getArtList({"id":artid}) TEMPLATE.update(setTempVar()) return template('article.html', TEMPLATE)
这里是可以正常获取到cookie的,而且代码没有任何区别.唯一的区别就是用户认证是跳转了页面.所以我help了一下:
from bottle import response help(response.set_cookie)
help的结果其中有两个参数一个是path,和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)
明显bottle的cookie默认只在当前路径下能读取的到,所以要别的页面读取到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 response.COOKIES['account']['path'] = '/admin' else: pass @route('/admin') def admin(): user = request.COOKIES['user']
这样我们就能在别的路径下访问我们设定的cookie.
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年10月09日
2024年10月09日
- 《原神》「星路拾忆」网页活动壁纸展示
- 群星.2024-祝你幸福!电影原声带【阿里巴巴】【FLAC分轨】
- 于台烟.2004-执着【乾坤唱片】【WAV+CUE】
- 黄品源.2004-感谢·情人(引进版)【滚石】【WAV+CUE】
- 《最终幻想16》评测:游、戏两难全的系列转折
- 《纸嫁衣》五部打包¥104,《鬼泣5》仅需¥44
- 《崩坏:星穹铁道》造物大赛卷出新高度
- 《王菲 菲常传奇 2CD》[WAV+CUE][950MB]
- 《王菲 空灵独味唱腔 非常传奇 紫水晶2CD》[WAV+CUE][860MB]
- 《车载专业测试王1号 极品音质》[WAV/分轨][980MB]
- 炉石传说的国服回归送什么奖励 炉石传说9.25回归最新消息介绍
- 云顶之弈最强阵容汇总2024 云顶之弈最强阵容搭配最新版本
- S14lol全球总决赛在哪观看 lol全球总决赛观赛直播地址汇总
- 黄思婷2011-感谢天[豪记唱片][WAV+CUE]
- 孙淑媚2013-太阳[台湾首版][WAV+CUE]