这两天为用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的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
