当前位置: 首页 > 编程笔记 >

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

蔺山
2023-03-14
本文向大家介绍Python的Bottle框架中获取制定cookie的教程,包括了Python的Bottle框架中获取制定cookie的教程的使用技巧和注意事项,需要的朋友参考一下

这两天为用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的Bottle框架基本知识总结,包括了Python的Bottle框架基本知识总结的使用技巧和注意事项,需要的朋友参考一下 基本映射 映射使用在根据不同URLs请求来产生相对应的返回内容.Bottle使用route() 修饰器来实现映射. 运行这个程序,访问http://localhost:8080/hello将会在浏览器里看到 "Hello World!". GET, P

  • 本文向大家介绍简单介绍Python的轻便web框架Bottle,包括了简单介绍Python的轻便web框架Bottle的使用技巧和注意事项,需要的朋友参考一下 基本映射 映射使用在根据不同URLs请求来产生相对应的返回内容.Bottle使用route() 修饰器来实现映射. 运行这个程序,访问http://localhost:8080/hello将会在浏览器里看到 "Hello World!".

  • 本文向大家介绍在Python的Bottle框架中使用微信API的示例,包括了在Python的Bottle框架中使用微信API的示例的使用技巧和注意事项,需要的朋友参考一下 微信这个东西估计宅男没几个不熟悉的吧,微信经过这么两年多的发展终于向开放平台跨出了友好的一步。蛋疼的以为微信会出一个详细的api等接口,兴奋不已的去申请了微信公共平台,然后开始找各种api的位置…… 花费了近一个小时,依然没找到

  • 本文向大家介绍Python的Bottle框架的一些使用技巧介绍,包括了Python的Bottle框架的一些使用技巧介绍的使用技巧和注意事项,需要的朋友参考一下 之前对bottle做过不少的介绍,也写过一些文章来说明bottle的缺点,最近发现其实之前有些地方说的不太公平,所以趁此机会也来更正一下。     bottle是支持类似flask url_for的语法的,具体使用方法在下文介绍     b

  • 本文向大家介绍Python的Flask框架中@app.route的用法教程,包括了Python的Flask框架中@app.route的用法教程的使用技巧和注意事项,需要的朋友参考一下 在我上一篇文章,我搭了一个框架,模拟了Flask网站上“@app.route(‘/')”第一条例子的行为。 如果你错过了那篇“这不是魔法”,请点击这里。 在这篇文章中,我们打算稍微调高点难度,为我们的URL加入可变参

  • 问题内容: 如何快速获取rightbarbuttonItem的框架?我发现了这一点:UIBarButtonItem:如何找到它的框架?但是它说不能将NSString转换为UIView,或者不能将NSString转换为String: 目标是删除rightBarButtonItem,添加一个imageView,然后使用fadeOut效果对其进行移动。 谢谢 问题答案: 您应该像这样尝试: 编辑(快速3