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登录QQ邮箱发信的实现代码
Feb 10 Python
python 写的一个爬虫程序源码
Feb 28 Python
在CentOS6上安装Python2.7的解决方法
Jan 09 Python
python学习基础之循环import及import过程
Apr 22 Python
Python 利用内置set函数对字符串和列表进行去重的方法
Jun 29 Python
Python判断以什么结尾以什么开头的实例
Oct 27 Python
Pandas之排序函数sort_values()的实现
Jul 09 Python
Python学习笔记之文件的读写操作实例分析
Aug 07 Python
Python中join()函数多种操作代码实例
Jan 13 Python
Python 随机生成测试数据的模块:faker基本使用方法详解
Apr 09 Python
python实现剪贴板的操作
Jul 01 Python
python基础之类属性和实例属性
Oct 24 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
php mysql索引问题
2008/06/07 PHP
PHP计算2点经纬度之间的距离代码
2013/08/12 PHP
CI(Codeigniter)的Setting增强配置类实例
2016/01/06 PHP
Zend Framework教程之响应对象的封装Zend_Controller_Response实例详解
2016/03/07 PHP
laravel 如何实现引入自己的函数或类库
2019/10/15 PHP
jquery.lazyload  实现图片延迟加载jquery插件
2010/02/06 Javascript
javascript自适应宽度的瀑布流实现思路
2013/02/20 Javascript
javascript轻量级模板引擎juicer使用指南
2014/06/22 Javascript
nodejs的10个性能优化技巧
2014/07/15 NodeJs
超详细的javascript数组方法汇总
2015/11/21 Javascript
jQuery实现鼠标滚动图片延迟加载效果附源码下载
2016/06/28 Javascript
JavaScript实现计算多边形质心的方法示例
2018/01/31 Javascript
jQuery实现碰到边缘反弹的动画效果
2018/02/24 jQuery
Vue-component全局注册实例
2018/09/06 Javascript
JS+CSS3实现的简易钟表效果示例
2019/04/13 Javascript
解决vue 退出动画无效的问题
2020/08/09 Javascript
antd-日历组件,前后禁止选择,只能选中间一部分的实例
2020/10/29 Javascript
react-native 实现购物车滑动删除效果的示例代码
2021/01/15 Javascript
使用vue3重构拼图游戏的实现示例
2021/01/25 Vue.js
python判断windows隐藏文件的方法
2014/03/21 Python
[原创]Python入门教程2. 字符串基本操作【运算、格式化输出、常用函数】
2018/10/29 Python
python实现字符串加密成纯数字
2019/03/19 Python
python通过robert、sobel、Laplace算子实现图像边缘提取详解
2019/08/21 Python
python实现统计代码行数的小工具
2019/09/19 Python
用python爬取历史天气数据的方法示例
2019/12/30 Python
TensorFlow使用Graph的基本操作的实现
2020/04/22 Python
python实现启动一个外部程序,并且不阻塞当前进程
2020/12/05 Python
python中HTMLParser模块知识点总结
2021/01/25 Python
全球最大化妆品零售网站:SkinStore
2020/10/24 全球购物
个人简历中自我评价
2014/02/11 职场文书
先进集体获奖感言
2014/02/13 职场文书
公休请假条
2014/04/11 职场文书
个人合作协议书范本
2014/04/18 职场文书
一年级小学生评语
2014/04/22 职场文书
讲解员培训方案
2014/05/04 职场文书
如何写股份合作协议书
2014/09/11 职场文书