Python的Bottle框架中猎取制定cookie的教程__第1页
Python的Bottle框架中猎取制定cookie的教程__第2页
Python的Bottle框架中猎取制定cookie的教程__第3页
Python的Bottle框架中猎取制定cookie的教程__第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Python的Bottle框架中猎取制定cookie的教程_ Python的Bottle框架中猎取制定cookie的教程 这篇文章主要介绍了Python的Bottle框架中猎取制定cookie的教程,主要是针对别的路径而不是当前页面的cookie,需要的伴侣可以参考下 这两天为用bottle+mongodb写的一个项目加上登录功能,无奈怎么都猎取不到保存的cookie,文档给出让我们这样操作cookie的代码片段: route(/login) def login (): username = request .forms .get(username ) password = request .

2、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, sec

3、ret= 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.POSTuser passwd = request.POSTpasswd if check_user_right(user,passwd): response.COOKIESaccount = user else

4、: pass route(/admin) def admin(): user = request.COOKIESuser 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+

5、ip+aid print request.COOKIES.keys() # 推断cookie是否存在 if artcookie in request.COOKIES.keys(): # 存在则更新有效时间 response.COOKIESartcookie = True response.COOKIESartcookiemax-age = 500 else: # 不存在则更新文章查看次数 db.posts.update(id:artid, $inc:views:1) # 并设置cookie response.COOKIESartcookie = True response.COOKIESart

6、cookiemax-age = 500 TEMPLATEposts = 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

7、 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.POSTuser passwd = request.POSTpasswd if check_user_right(user,passwd): response.COOKIESaccount = user response.COOKIESaccountpath =

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论