Python的Bottle框架中获取制定cookie的教程
作者:C Wong 时间:2021-06-28 02:34:20
这两天为用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
0
投稿
猜你喜欢
pyecharts绘制时间轮播图柱形图+饼图+玫瑰图+折线图
2022-01-18 13:19:47
JS复制特定内容到粘贴板
2011-04-02 11:09:00
jQuery+php简单实现全选删除的方法
2023-11-05 20:23:38
Python3实现英文字母转换哥特式字体实例代码
2023-08-04 10:28:11
python日期相关操作实例小结
2021-07-14 18:39:13
python爬虫 urllib模块发起post请求过程解析
2022-02-07 22:52:53
Python实现string字符串连接的方法总结【8种方式】
2023-01-07 20:52:29
PHP echo()函数讲解
2023-06-05 18:50:54
一个不错网速测试代码
2008-07-20 13:41:00
oracle 下WITH CHECK OPTION用法
2009-02-28 10:59:00
详解Scrapy Redis入门实战
2023-04-14 11:39:08
python+pytest接口自动化之日志管理模块loguru简介
2021-10-25 14:41:34
Python安装教程全过程(2022最新)
2022-05-09 06:10:55
python3 wechatpy微信支付的项目实践
2023-08-29 14:13:36
Python使用openpyxl复制整张sheet
2023-11-21 23:09:18
Python获取一个用户名的组ID过程解析
2021-09-04 15:40:05
Python yield生成器和return对比代码实例
2022-07-17 21:54:57
Python实现贪吃蛇小游戏(单人模式)
2023-09-26 23:14:42
asp自动采集程序
2009-02-04 10:11:00
Python办公自动化之Excel介绍
2021-04-19 11:06:07