神剑山庄资源网 Design By www.hcban.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.
标签:
Python
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无Python的Bottle框架中获取制定cookie的教程的评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年10月03日
2024年10月03日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】