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 相关文章推荐
Swift 3.0在集合类数据结构上的一些新变化总结
Jul 11 Python
Python中的命令行参数解析工具之docopt详解
Mar 27 Python
对Python3中的input函数详解
Apr 22 Python
Python基于SMTP协议实现发送邮件功能详解
Aug 14 Python
pandas 条件搜索返回列表的方法
Oct 30 Python
python Selenium实现付费音乐批量下载的实现方法
Jan 24 Python
使用Python向DataFrame中指定位置添加一列或多列的方法
Jan 29 Python
Python提取特定时间段内数据的方法实例
Apr 01 Python
django的model操作汇整详解
Jul 26 Python
Apache部署Django项目图文详解
Jul 30 Python
python两种注释用法的示例
Oct 09 Python
Python 操作pdf pdfplumber读取PDF写入Exce
Aug 14 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
linux下实现定时执行php脚本
2015/02/13 PHP
php实现word转html的方法
2016/01/22 PHP
Yii2中设置与获取别名的函数(setAlias和getAlias)用法分析
2016/07/25 PHP
PHP实现的AES双向加密解密功能示例【128位】
2018/09/03 PHP
php设计模式之职责链模式定义与用法经典示例
2019/09/19 PHP
Javascript 中的 call 和 apply使用介绍
2012/02/22 Javascript
javaScript 删除字符串空格多种方法小结
2012/10/24 Javascript
关于jquery input textare 事件绑定及用法学习
2013/04/03 Javascript
document.write的几点使用心得
2014/05/14 Javascript
js解决select下拉选不中问题
2014/10/14 Javascript
JS实现简易图片轮播效果的方法
2015/03/25 Javascript
JavaScript里四舍五入函数round用法实例
2015/04/06 Javascript
JavaScript Split()方法
2015/12/18 Javascript
用JS实现轮播图效果(二)
2016/06/26 Javascript
JavaScript实现倒计时跳转页面功能【实用】
2016/12/13 Javascript
jquery submit()不能提交表单的解决方法
2017/04/24 jQuery
详解Angular 4.x Injector
2017/05/04 Javascript
Angular中自定义Debounce Click指令防止重复点击
2017/07/26 Javascript
浅谈JS获取元素的N种方法及其动静态讨论
2017/08/25 Javascript
react 应用多入口配置及实践总结
2018/10/17 Javascript
详解webpack+ES6+Sass搭建多页面应用
2018/11/05 Javascript
js加减乘除精确运算方法实例代码
2021/01/17 Javascript
浅谈Python的文件类型
2016/05/30 Python
django 控制页面跳转的例子
2019/08/06 Python
Django CSRF认证的几种解决方案
2020/03/03 Python
Pycharm如何导入python文件及解决报错问题
2020/05/10 Python
python使用hdfs3模块对hdfs进行操作详解
2020/06/06 Python
在Ubuntu中安装并配置Pycharm教程的实现方法
2021/01/06 Python
css3实现可拖动的魔方3d效果
2019/05/07 HTML / CSS
Html5适配iphoneX刘海屏的简单实现
2019/04/09 HTML / CSS
Myprotein加拿大官网:欧洲第一的运动营养品牌
2018/01/06 全球购物
澳大利亚设计的优质鞋类和适合澳大利亚生活方式的服装:Rivers
2019/04/23 全球购物
英国复古服装购物网站:Collectif
2019/10/30 全球购物
掌上明珠Java程序员面试总结
2016/02/23 面试题
预备党员个人总结
2015/02/14 职场文书
Go获取两个时区的时间差
2022/04/20 Golang