How to use the quart.websocket function in Quart

To help you get started, we’ve selected a few Quart examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pgjones / quart / tests / test_blueprints.py View on Github external
async def ws() -> None:
        while True:
            await websocket.send(websocket.blueprint.encode())
github python-gino / gino / tests / test_quart.py View on Github external
async def ws():
        while True:
            data = json.loads(await websocket.receive())
            action = data.get('action')
            if action == 'add':
                new_user = await _add_user(None, data.get('name'))
                await websocket.send(json.dumps(new_user))
            elif action == 'get':
                try:
                    user = await _get_user(
                        None, int(data.get('id')), data.get('method'))
                except NotFound:
                    await websocket.send(json.dumps({'error': 'not found'}))
                else:
                    await websocket.send(json.dumps(user))
            else:
                await websocket.send(json.dumps({'error': 'Invalid JSON'}))
github richardchien / python-aiocqhttp / aiocqhttp / __init__.py View on Github external
def _add_ws_reverse_api_connection(self) -> None:
        # noinspection PyProtectedMember
        ws = websocket._get_current_object()
        self_id = websocket.headers.get('X-Self-ID', '*')
        self._connected_ws_reverse_api_clients[self_id] = ws
github richardchien / python-aiocqhttp / aiocqhttp / api.py View on Github external
def _is_available(self) -> bool:
        # available only when current event ws has a corresponding api ws
        return event_ws and event_ws.headers.get(
            'X-Self-ID', '*') in self._connected_clients
github richardchien / python-aiocqhttp / aiocqhttp / __init__.py View on Github external
async def _handle_ws_reverse(self):
        self._validate_ws_reverse_access_token()

        role = websocket.headers.get('X-Client-Role', '').lower()
        if role == 'event':
            await self._handle_ws_reverse_event()
        elif role == 'api':
            await self._handle_ws_reverse_api()
        elif role == 'universal':
            await self._handle_ws_reverse_universal()
github pgjones / quart / examples / websocket / websocket.py View on Github external
async def ws():
    while True:
        data = await websocket.receive()
        await websocket.send(f"echo {data}")
github pgjones / quart / examples / websocket / websocket.py View on Github external
async def ws():
    while True:
        data = await websocket.receive()
        await websocket.send(f"echo {data}")
github richardchien / python-aiocqhttp / aiocqhttp / __init__.py View on Github external
def _validate_access_token(self):
        if not self._access_token:
            return

        if websocket:
            auth = websocket.headers.get('Authorization', '')
            if not auth.startswith('Token ') and not auth.startswith('token '):
                abort(401)

            token_given = auth[len('Token '):].strip()
            if not token_given:
                abort(401)
            if token_given != self._access_token:
                abort(403)