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多线程方式执行多个bat代码
Jun 07 Python
老生常谈python的私有公有属性(必看篇)
Jun 09 Python
Flask解决跨域的问题示例代码
Feb 12 Python
浅析python打包工具distutils、setuptools
Apr 20 Python
Python 统计字数的思路详解
May 08 Python
Python判断一个三位数是否为水仙花数的示例
Nov 13 Python
python3.6实现学生信息管理系统
Feb 21 Python
Python定时任务工具之APScheduler使用方式
Jul 24 Python
Python将string转换到float的实例方法
Jul 29 Python
Windows 下更改 jupyterlab 默认启动位置的教程详解
May 18 Python
Python是怎样处理json模块的
Jul 16 Python
python字典的元素访问实例详解
Jul 21 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 输出双引号"与单引号'的方法
2010/05/09 PHP
php计算程序运行时间的简单例子分享
2014/05/10 PHP
PHP实现过滤掉非汉字字符只保留中文字符
2015/06/04 PHP
php实现基于PDO的预处理示例
2017/03/28 PHP
php获取微信共享收货地址的方法
2017/12/21 PHP
javascript 清除输入框中的数据
2009/04/13 Javascript
Extjs Ext.MessageBox.confirm 确认对话框详解
2010/04/02 Javascript
JQuery的Ajax跨域请求原理概述及实例
2013/04/26 Javascript
sencha touch 模仿tabpanel导航栏TabBar的实例代码
2013/10/24 Javascript
checkbox全选所涉及到的知识点介绍
2013/12/31 Javascript
Js使用WScript.Shell对象执行.bat文件和cmd命令
2014/12/18 Javascript
JQuery分屏指示器图片轮换效果实例
2015/05/21 Javascript
js获取客户端操作系统类型的方法【测试可用】
2016/05/27 Javascript
微信小程序获取微信运动步数的实例代码
2017/07/20 Javascript
JS实现浏览上传文件的代码
2017/08/23 Javascript
从对象列表中获取一个对象的方法,依据关键字和值
2017/09/20 Javascript
如何用webpack4带你实现一个vue的打包的项目
2018/06/20 Javascript
解决layui中table异步数据请求不支持自定义返回数据格式的问题
2018/08/19 Javascript
Vue动态加载图片在跨域时无法显示的问题及解决方法
2020/03/10 Javascript
Vue实现购物车实例代码两则
2020/05/30 Javascript
Openlayers显示瓦片网格信息的方法
2020/09/28 Javascript
python和ruby,我选谁?
2017/09/13 Python
python实现在IDLE中输入多行的方法
2018/04/19 Python
pandas实现将dataframe满足某一条件的值选出
2019/06/12 Python
Pycharm编辑器功能之代码折叠效果的实现代码
2020/10/15 Python
python基于socket模拟实现ssh远程执行命令
2020/12/05 Python
CSS3 Backgrounds属性相关介绍
2011/05/11 HTML / CSS
Html5上传图片 移动端、PC端通用代码
2016/06/08 HTML / CSS
Merrell美国官网:美国登山运动鞋品牌
2018/02/07 全球购物
经济学人订阅:The Economist
2018/07/19 全球购物
小学开学寄语
2014/01/19 职场文书
买房委托公证书
2014/04/08 职场文书
党员自我剖析材料
2014/08/31 职场文书
小学音乐教师个人工作总结
2015/02/05 职场文书
2015年双拥工作总结
2015/04/08 职场文书
Python turtle编写简单的球类小游戏
2022/03/31 Python