This repository was archived by the owner on Nov 23, 2017. It is now read-only.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 185
This repository was archived by the owner on Nov 23, 2017. It is now read-only.
How to keep session alive when using async websockets?#491
Copy link
Copy link
Open
Description
I have this code to interact with a websocket api using async and websokets python libraries:
#!/usr/bin/env python3 import sys, json import asyncio from websockets import connect class AsyncWebsocket(): async def __aenter__(self): self._conn = connect('wss://ws.myws.com/v2') self.websocket = await self._conn.__aenter__() return self async def __aexit__(self, *args, **kwargs): await self._conn.__aexit__(*args, **kwargs) async def send(self, message): await self.websocket.send(message) async def receive(self): return await self.websocket.recv() class mtest(): def __init__(self, api_token): self.aws = AsyncWebsocket() self.loop = asyncio.get_event_loop() self.api_token = api_token self.authorize() def authorize(self): jdata = self.__async_exec({'authorize': self.api_token }) try: print (jdata['email']) ret = True except: ret = False return ret def sendtest(self): jdata = self.__async_exec({"hello": 1 }) print (jdata) def __async_exec(self, jdata): try: ret = json.loads(self.loop.run_until_complete(self.__async_send_recieve(jdata))) except: ret = None return ret async def __async_send_recieve(self, jdata): async with self.aws: await self.aws.send(json.dumps(jdata)) return await self.aws.receive() So I have the following in main.py:
from webclass import * a = mtest('12341234') print (a.sendtest()) The problem is that it doesn't preserve the authorized session, so this is the output:
root@ubupc1:/home/dinocob# python3 test.py [email protected]{'error':{'message': 'Please log in.', 'code': 'AuthorizationRequired'}} As you see, the login call are working ok, but when calling and sending the hello in sendtest function, the session is not the same.
- Where is destroyed the session?
- How could I preserve it (without drastically
modifying my class structure)?
mllamazares
Metadata
Metadata
Assignees
Labels
No labels