当前位置: 首页>>代码示例>>Python>>正文


Python aiohttp_session.get_session方法代码示例

本文整理汇总了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} 
开发者ID:aio-libs,项目名称:aiohttp_admin,代码行数:22,代码来源:views.py

示例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) 
开发者ID:aio-libs,项目名称:aiohttp_admin,代码行数:18,代码来源:views.py

示例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') 
开发者ID:aio-libs,项目名称:aiohttp_admin,代码行数:22,代码来源:views.py

示例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 
开发者ID:aio-libs,项目名称:aiohttp-session,代码行数:19,代码来源:login_required_example.py

示例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'] 
开发者ID:aio-libs,项目名称:aiohttp-session,代码行数:26,代码来源:test_memcached_storage.py

示例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" 
开发者ID:aio-libs,项目名称:aiohttp-session,代码行数:21,代码来源:test_memcached_storage.py

示例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' 
开发者ID:aio-libs,项目名称:aiohttp-session,代码行数:24,代码来源:test_memcached_storage.py

示例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 
开发者ID:aio-libs,项目名称:aiohttp-session,代码行数:24,代码来源:test_memcached_storage.py

示例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 
开发者ID:aio-libs,项目名称:aiohttp-session,代码行数:25,代码来源:test_memcached_storage.py

示例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'] 
开发者ID:aio-libs,项目名称:aiohttp-session,代码行数:26,代码来源:test_redis_storage.py

示例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" 
开发者ID:aio-libs,项目名称:aiohttp-session,代码行数:20,代码来源:test_redis_storage.py

示例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 
开发者ID:aio-libs,项目名称:aiohttp-session,代码行数:27,代码来源:test_redis_storage.py

示例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 
开发者ID:aio-libs,项目名称:aiohttp-session,代码行数:21,代码来源:test_redis_storage.py

示例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' 
开发者ID:aio-libs,项目名称:aiohttp-session,代码行数:22,代码来源:test_redis_storage.py

示例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 
开发者ID:aio-libs,项目名称:aiohttp-session,代码行数:24,代码来源:test_redis_storage.py


注:本文中的aiohttp_session.get_session方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。