Python的Bottle框架中获取制定cookie的教程


Posted in Python onApril 24, 2015

这两天为用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 相关文章推荐
浅谈python中的正则表达式(re模块)
Oct 17 Python
13个最常用的Python深度学习库介绍
Oct 28 Python
Python与R语言的简要对比
Nov 14 Python
python爬虫获取淘宝天猫商品详细参数
Jun 23 Python
Python使用修饰器进行异常日志记录操作示例
Mar 19 Python
Django利用cookie保存用户登录信息的简单实现方法
May 27 Python
django项目环境搭建及在虚拟机本地创建django项目的教程
Aug 02 Python
Python 分发包中添加额外文件的方法
Aug 16 Python
Python倒排索引之查找包含某主题或单词的文件
Nov 13 Python
浅谈keras中的Merge层(实现层的相加、相减、相乘实例)
May 23 Python
matplotlib设置颜色、标记、线条,让你的图像更加丰富(推荐)
Sep 25 Python
python3+PyQt5+Qt Designer实现界面可视化
Jun 10 Python
利用Python的装饰器解决Bottle框架中用户验证问题
Apr 24 #Python
在Python中使用mongoengine操作MongoDB教程
Apr 24 #Python
python使用arp欺骗伪造网关的方法
Apr 24 #Python
python使用wxPython打开并播放wav文件的方法
Apr 24 #Python
python使用PyGame播放Midi和Mp3文件的方法
Apr 24 #Python
python使用PyGame绘制图像并保存为图片文件的方法
Apr 24 #Python
python使用PIL缩放网络图片并保存的方法
Apr 24 #Python
You might like
水质对咖图啡风味的影响具体有哪些
2021/03/03 冲泡冲煮
php数组中删除元素的实现代码
2012/06/22 PHP
PHP实现多进程并行操作的详解(可做守护进程)
2013/06/18 PHP
PHP在同一域名下两个不同的项目做独立登录机制详解
2017/09/22 PHP
Yii2.0实现的批量更新及批量插入功能示例
2019/01/29 PHP
模仿JQuery sortable效果 代码有错但值得看看
2009/11/05 Javascript
动感效果的TAB选项卡jquery 插件
2011/07/09 Javascript
让AJAX不依赖后端接口实现方案
2012/12/03 Javascript
js的匿名函数使用介绍
2013/12/11 Javascript
Webpack+Vue如何导入Jquery和Jquery的第三方插件
2017/02/20 Javascript
Vuex模块化实现待办事项的状态管理
2017/03/15 Javascript
利用Vue.js实现求职在线之职位查询功能
2017/07/03 Javascript
详解tween.js的使用教程
2017/09/14 Javascript
原生JS+CSS实现炫酷重力模拟弹跳系统的登录页面
2017/11/01 Javascript
jQuery实现form表单序列化转换为json对象功能示例
2018/05/23 jQuery
node实现socket链接与GPRS进行通信的方法
2019/05/20 Javascript
js常用方法、检查是否有特殊字符串、倒序截取字符串操作完整示例
2020/01/26 Javascript
JS实现随机点名器
2020/04/12 Javascript
[49:20]VG vs TNC Supermajor小组赛B组败者组决赛 BO3 第二场 6.2
2018/06/03 DOTA
简单的连接MySQL与Python的Bottle框架的方法
2015/04/30 Python
Python获取当前路径实现代码
2017/05/08 Python
解决Django一个表单对应多个按钮的问题
2019/07/18 Python
Windows下python3安装tkinter的问题及解决方法
2020/01/06 Python
在django中查询获取数据,get, filter,all(),values()操作
2020/08/09 Python
CSS3实现翘边的阴影效果的代码示例
2016/06/13 HTML / CSS
html5中如何将图片的绝对路径转换成文件对象
2018/01/11 HTML / CSS
Android面试题附答案
2014/12/08 面试题
索引覆盖(Index Covering)查询含义
2012/02/18 面试题
公司年会晚宴演讲稿
2014/01/06 职场文书
水果超市创业计划书
2014/01/27 职场文书
节水标语大全
2014/06/11 职场文书
教师政风行风自查自纠报告
2014/10/21 职场文书
寒假安全保证书
2015/02/28 职场文书
军训后的感想
2015/08/07 职场文书
Redis安装启动及常见数据类型
2021/04/14 Redis
PostgreSQL自动更新时间戳实例代码
2021/11/27 PostgreSQL