问题描述
第一次提问者.
我目前正在努力使用Bottle微型框架正确使用Beaker.这是有问题的程序:
I'm currently struggling on how to use Beaker properly using the Bottle micro-framework. Here's the problematic program:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# filename: server.py
import bottle as app
from beaker.middleware import SessionMiddleware
session_options = {
'session.type': 'file',
'session.data_dir': './session/',
'session.auto': True,
}
app_middlware = SessionMiddleware(app.app(), session_options)
app_session = app.request.environ.get('beaker.session')
@app.route('/login')
def login():
app_session = app.request.environ.get('beaker.session')
app_session['logged_in'] = True
@app.route('/logout')
def logout():
app_session = app.request.environ.get('beaker.session')
if app_session.get('logged_in'):
app_session['logged_in'] = False
return 'You are logged out'
app.redirect('/login')
@app.route('/dashboard')
def dashboard():
app_session = app.request.environ.get('beaker.session')
if app_session.get('logged_in'):
return 'You are logged in'
app.redirect('/login')
app.debug(True)
app.run(app=app_middlware, reloader=True)
如果您注意到了,我会继续在每个def
块上调用app_session = app.request.environ.get('beaker.session')
,这样它就不会返回类似以下的错误:TypeError: 'NoneType' object does not support item assignment
---似乎Python无法识别其功能之外的变量(如果我错了,请纠正我).
If you noticed, I keep on calling app_session = app.request.environ.get('beaker.session')
on every def
block so just it will not return an error like: TypeError: 'NoneType' object does not support item assignment
--- it seems that Python doesn't recognize variables that is outside its function (correct me if I'm wrong).
这是问题:
- 我应该怎么做才能只有一个
app_session = app.request.environ.get('beaker.session')
实例,以便每个def
块都可以使用它(我真的需要一个实例,因为它是要检查和使用的同一会话). - 如果这是唯一的方法(虽然很丑),那么我是否应该仅合并需要会话的所有路由,以便可以实现
app_session
的单个实例?
- What should I do to only have one instance of
app_session = app.request.environ.get('beaker.session')
so it can be available to everydef
blocks (I really need one instance since it's the same session to be checked and used). - If this is the only way (it's ugly though), then should I just combine all routes that requires a session just so I can achieve the single instance of
app_session
?
类似的东西:
@app.route('/<path:re:(login|dashboard|logout)\/?>')
def url(path):
app_session = app.request.environ.get('beaker.session')
if 'login' in path:
app_session['logged_in'] = True
elif 'logout' in path:
if app_session.get('logged_in'):
# app_session.delete() it doesn't seem to work?
app_session['logged_in'] = False
return 'You are logged out'
app.redirect('/login')
elif 'dashboard' in path:
if app_session.get('logged_in'):
return 'You are logged in'
app.redirect('/login')
推荐答案
在bottle
应用程序中使用beaker
很容易.首先,设置您的Bottle应用程序:
Using beaker
in your bottle
application is easy. First, set up your Bottle app:
import bottle
from bottle import request, route, hook
import beaker.middleware
session_opts = {
'session.type': 'file',
'session.data_dir': './session/',
'session.auto': True,
}
app = beaker.middleware.SessionMiddleware(bottle.app(), session_opts)
随后:
bottle.run(app=app)
有了这个选项,每次您收到请求时,您的Beaker会话都将以request.environ['beaker_session']
.为了方便起见,我通常会这样做:
With this in place, every time you receive a request, your Beaker session will be available asrequest.environ['beaker_session']
. I usually do something like this for convenience:
@hook('before_request')
def setup_request():
request.session = request.environ['beaker.session']
这可以安排在处理任何请求之前运行setup_request
;我们使用的是bottle.request
变量(请参见前面的import语句),它是线程局部变量,其中包含有关当前请求的信息.从现在开始,只要需要,我就可以引用request.session
,例如:
This arranges to run setup_request
before handling any request; we're using the bottle.request
variable (see the earlier import statement), which is a thread-local variable with information about the current request. From this point on, I can just refer to request.session
whenever I need it, e.g.:
@route('/')
def index():
if 'something' in request.session:
return 'It worked!'
request.session['something'] = 1
这篇关于与Beaker进行Bottle.py会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!