本文整理汇总了Python中aiohttp_session.get_session方法的典型用法代码示例。如果您正苦于以下问题:Python aiohttp_session.get_session方法的具体用法?Python aiohttp_session.get_session怎么用?Python aiohttp_session.get_session使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp_session
的用法示例。
在下文中一共展示了aiohttp_session.get_session方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: login
# 需要导入模块: import aiohttp_session [as 别名]
# 或者: from aiohttp_session import get_session [as 别名]
def login(self, request):
session = await get_session(request)
user_id = session.get('user_id')
if user_id:
return redirect(request, 'timeline')
error = None
form = None
if request.method == 'POST':
form = await request.post()
user = await self.mongo.user.find_one(
{'username': form['username']})
if user is None:
error = 'Invalid username'
elif not check_password_hash(user['pw_hash'], form['password']):
error = 'Invalid password'
else:
session['user_id'] = str(user['_id'])
return redirect(request, 'timeline')
return {"error": error, "form": form}
示例2: unfollow_user
# 需要导入模块: import aiohttp_session [as 别名]
# 或者: from aiohttp_session import get_session [as 别名]
def unfollow_user(self, request):
"""Removes the current user as follower of the given user."""
username = request.match_info['username']
session = await get_session(request)
user_id = session.get('user_id')
if not user_id:
raise web.HTTPNotAuthorized()
whom_id = await db.get_user_id(self.mongo.user, username)
if whom_id is None:
raise web.HTTPFound()
await self.mongo.follower.update_many(
{'who_id': ObjectId(session['user_id'])},
{'$pull': {'whom_id': whom_id}})
return redirect(request, 'user_timeline', username=username)
示例3: add_message
# 需要导入模块: import aiohttp_session [as 别名]
# 或者: from aiohttp_session import get_session [as 别名]
def add_message(self, request):
"""Registers a new message for the user."""
session = await get_session(request)
user_id = session.get('user_id')
if not user_id:
raise web.HTTPNotAuthorized()
form = await request.post()
if form.get('text'):
user = await self.mongo.user.find_one(
{'_id': ObjectId(session['user_id'])},
{'email': 1, 'username': 1})
await self.mongo.message.insert_one(
{'author_id': ObjectId(user_id),
'email': user['email'],
'username': user['username'],
'text': form['text'],
'pub_date': datetime.datetime.utcnow()})
return redirect(request, 'timeline')
示例4: login_required
# 需要导入模块: import aiohttp_session [as 别名]
# 或者: from aiohttp_session import get_session [as 别名]
def login_required(fn):
async def wrapped(request, *args, **kwargs):
app = request.app
router = app.router
session = await get_session(request)
if 'user_id' not in session:
return web.HTTPFound(router['login'].url_for())
user_id = session['user_id']
# actually load user from your database (e.g. with aiopg)
user = DATABASE[user_id]
app['user'] = user
return await fn(request, *args, **kwargs)
return wrapped
示例5: test_change_session
# 需要导入模块: import aiohttp_session [as 别名]
# 或者: from aiohttp_session import get_session [as 别名]
def test_change_session(aiohttp_client, memcached):
async def handler(request):
session = await get_session(request)
session['c'] = 3
return web.Response(body=b'OK')
client = await aiohttp_client(create_app(handler, memcached))
await make_cookie(client, memcached, {'a': 1, 'b': 2})
resp = await client.get('/')
assert resp.status == 200
value = await load_cookie(client, memcached)
assert 'session' in value
assert 'a' in value['session']
assert 'b' in value['session']
assert 'c' in value['session']
assert 'created' in value
assert value['session']['a'] == 1
assert value['session']['b'] == 2
assert value['session']['c'] == 3
morsel = resp.cookies['AIOHTTP_SESSION']
assert morsel['httponly']
assert '/' == morsel['path']
示例6: test_clear_cookie_on_session_invalidation
# 需要导入模块: import aiohttp_session [as 别名]
# 或者: from aiohttp_session import get_session [as 别名]
def test_clear_cookie_on_session_invalidation(aiohttp_client,
memcached):
async def handler(request):
session = await get_session(request)
session.invalidate()
return web.Response(body=b'OK')
client = await aiohttp_client(create_app(handler, memcached))
await make_cookie(client, memcached, {'a': 1, 'b': 2})
resp = await client.get('/')
assert resp.status == 200
value = await load_cookie(client, memcached)
assert {} == value
morsel = resp.cookies['AIOHTTP_SESSION']
assert morsel['path'] == '/'
assert morsel['expires'] == "Thu, 01 Jan 1970 00:00:00 GMT"
assert morsel['max-age'] == "0"
示例7: test_create_storage_with_custom_key_factory
# 需要导入模块: import aiohttp_session [as 别名]
# 或者: from aiohttp_session import get_session [as 别名]
def test_create_storage_with_custom_key_factory(aiohttp_client,
memcached):
async def handler(request):
session = await get_session(request)
session['key'] = 'value'
assert session.new
return web.Response(body=b'OK')
def key_factory():
return 'test-key'
client = await aiohttp_client(create_app(handler, memcached, 8,
key_factory))
resp = await client.get('/')
assert resp.status == 200
assert resp.cookies['AIOHTTP_SESSION'].value == 'test-key'
value = await load_cookie(client, memcached)
assert 'key' in value['session']
assert value['session']['key'] == 'value'
示例8: test_memcached_session_fixation
# 需要导入模块: import aiohttp_session [as 别名]
# 或者: from aiohttp_session import get_session [as 别名]
def test_memcached_session_fixation(aiohttp_client, memcached):
async def login(request):
session = await get_session(request)
session['k'] = 'v'
return web.Response()
async def logout(request):
session = await get_session(request)
session.invalidate()
return web.Response()
app = create_app(login, memcached)
app.router.add_route('DELETE', '/', logout)
client = await aiohttp_client(app)
resp = await client.get('/')
assert 'AIOHTTP_SESSION' in resp.cookies
evil_cookie = resp.cookies['AIOHTTP_SESSION'].value
resp = await client.delete('/')
assert resp.cookies['AIOHTTP_SESSION'].value == ""
client.session.cookie_jar.update_cookies({'AIOHTTP_SESSION': evil_cookie})
resp = await client.get('/')
assert resp.cookies['AIOHTTP_SESSION'].value != evil_cookie
示例9: test_load_session_dont_load_expired_session
# 需要导入模块: import aiohttp_session [as 别名]
# 或者: from aiohttp_session import get_session [as 别名]
def test_load_session_dont_load_expired_session(aiohttp_client,
memcached):
async def handler(request):
session = await get_session(request)
exp_param = request.rel_url.query.get('exp', None)
if exp_param is None:
session['a'] = 1
session['b'] = 2
else:
assert {} == session
return web.Response(body=b'OK')
client = await aiohttp_client(
create_app(handler, memcached, 2)
)
resp = await client.get('/')
assert resp.status == 200
await asyncio.sleep(5)
resp = await client.get('/?exp=yes')
assert resp.status == 200
示例10: test_change_session
# 需要导入模块: import aiohttp_session [as 别名]
# 或者: from aiohttp_session import get_session [as 别名]
def test_change_session(aiohttp_client, redis):
async def handler(request):
session = await get_session(request)
session['c'] = 3
return web.Response(body=b'OK')
client = await aiohttp_client(create_app(handler, redis))
await make_cookie(client, redis, {'a': 1, 'b': 2})
resp = await client.get('/')
assert resp.status == 200
value = await load_cookie(client, redis)
assert 'session' in value
assert 'a' in value['session']
assert 'b' in value['session']
assert 'c' in value['session']
assert 'created' in value
assert value['session']['a'] == 1
assert value['session']['b'] == 2
assert value['session']['c'] == 3
morsel = resp.cookies['AIOHTTP_SESSION']
assert morsel['httponly']
assert '/' == morsel['path']
示例11: test_clear_cookie_on_session_invalidation
# 需要导入模块: import aiohttp_session [as 别名]
# 或者: from aiohttp_session import get_session [as 别名]
def test_clear_cookie_on_session_invalidation(aiohttp_client, redis):
async def handler(request):
session = await get_session(request)
session.invalidate()
return web.Response(body=b'OK')
client = await aiohttp_client(create_app(handler, redis))
await make_cookie(client, redis, {'a': 1, 'b': 2})
resp = await client.get('/')
assert resp.status == 200
value = await load_cookie(client, redis)
assert {} == value
morsel = resp.cookies['AIOHTTP_SESSION']
assert morsel['path'] == '/'
assert morsel['expires'] == "Thu, 01 Jan 1970 00:00:00 GMT"
assert morsel['max-age'] == "0"
示例12: test_create_cookie_in_handler
# 需要导入模块: import aiohttp_session [as 别名]
# 或者: from aiohttp_session import get_session [as 别名]
def test_create_cookie_in_handler(aiohttp_client, redis):
async def handler(request):
session = await get_session(request)
session['a'] = 1
session['b'] = 2
return web.Response(body=b'OK', headers={'HOST': 'example.com'})
client = await aiohttp_client(create_app(handler, redis))
resp = await client.get('/')
assert resp.status == 200
value = await load_cookie(client, redis)
assert 'session' in value
assert 'a' in value['session']
assert 'b' in value['session']
assert 'created' in value
assert value['session']['a'] == 1
assert value['session']['b'] == 2
morsel = resp.cookies['AIOHTTP_SESSION']
assert morsel['httponly']
assert morsel['path'] == '/'
with await redis as conn:
exists = await conn.exists('AIOHTTP_SESSION_' + morsel.value)
assert exists
示例13: test_set_ttl_manually_set
# 需要导入模块: import aiohttp_session [as 别名]
# 或者: from aiohttp_session import get_session [as 别名]
def test_set_ttl_manually_set(aiohttp_client, redis):
async def handler(request):
session = await get_session(request)
session.max_age = 10
session['a'] = 1
return web.Response(body=b'OK')
client = await aiohttp_client(create_app(handler, redis))
resp = await client.get('/')
assert resp.status == 200
key = resp.cookies['AIOHTTP_SESSION'].value
with await redis as conn:
ttl = await conn.ttl('AIOHTTP_SESSION_'+key)
assert ttl > 9
assert ttl <= 10
示例14: test_create_storage_with_custom_key_factory
# 需要导入模块: import aiohttp_session [as 别名]
# 或者: from aiohttp_session import get_session [as 别名]
def test_create_storage_with_custom_key_factory(aiohttp_client, redis):
async def handler(request):
session = await get_session(request)
session['key'] = 'value'
assert session.new
return web.Response(body=b'OK')
def key_factory():
return 'test-key'
client = await aiohttp_client(create_app(handler, redis, 8, key_factory))
resp = await client.get('/')
assert resp.status == 200
assert resp.cookies['AIOHTTP_SESSION'].value == 'test-key'
value = await load_cookie(client, redis)
assert 'key' in value['session']
assert value['session']['key'] == 'value'
示例15: test_redis_session_fixation
# 需要导入模块: import aiohttp_session [as 别名]
# 或者: from aiohttp_session import get_session [as 别名]
def test_redis_session_fixation(aiohttp_client, redis):
async def login(request):
session = await get_session(request)
session['k'] = 'v'
return web.Response()
async def logout(request):
session = await get_session(request)
session.invalidate()
return web.Response()
app = create_app(login, redis)
app.router.add_route('DELETE', '/', logout)
client = await aiohttp_client(app)
resp = await client.get('/')
assert 'AIOHTTP_SESSION' in resp.cookies
evil_cookie = resp.cookies['AIOHTTP_SESSION'].value
resp = await client.delete('/')
assert resp.cookies['AIOHTTP_SESSION'].value == ""
client.session.cookie_jar.update_cookies({'AIOHTTP_SESSION': evil_cookie})
resp = await client.get('/')
assert resp.cookies['AIOHTTP_SESSION'].value != evil_cookie