Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def ws() -> None:
while True:
await websocket.send(websocket.blueprint.encode())
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'}))
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
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
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()
async def ws():
while True:
data = await websocket.receive()
await websocket.send(f"echo {data}")
async def ws():
while True:
data = await websocket.receive()
await websocket.send(f"echo {data}")
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)