Title: How to keep session alive when using async websockets? · Issue #491 · python/asyncio · GitHub
Open Graph Title: How to keep session alive when using async websockets? · Issue #491 · python/asyncio
X Title: How to keep session alive when using async websockets? · Issue #491 · python/asyncio
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....
Open Graph 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 AsyncWebsoc...
X 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 AsyncWebsoc...
Opengraph URL: https://github.com/python/asyncio/issues/491
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"How to keep session alive when using async websockets?","articleBody":"I have this code to interact with a websocket api using `async` and `websokets` python libraries:\r\n\r\n```\r\n#!/usr/bin/env python3\r\n\r\nimport sys, json\r\nimport asyncio\r\nfrom websockets import connect\r\n\r\nclass AsyncWebsocket():\r\n async def __aenter__(self):\r\n self._conn = connect('wss://ws.myws.com/v2')\r\n self.websocket = await self._conn.__aenter__() \r\n return self\r\n\r\n async def __aexit__(self, *args, **kwargs):\r\n await self._conn.__aexit__(*args, **kwargs)\r\n\r\n async def send(self, message):\r\n await self.websocket.send(message)\r\n\r\n async def receive(self):\r\n return await self.websocket.recv()\r\n\r\nclass mtest():\r\n def __init__(self, api_token):\r\n self.aws = AsyncWebsocket()\r\n self.loop = asyncio.get_event_loop()\r\n self.api_token = api_token\r\n \r\n self.authorize()\r\n\r\n def authorize(self):\r\n jdata = self.__async_exec({\r\n 'authorize': self.api_token\r\n })\r\n\r\n try:\r\n print (jdata['email'])\r\n ret = True\r\n except:\r\n ret = False\r\n\r\n return ret\r\n\r\n def sendtest(self):\r\n\r\n jdata = self.__async_exec({\r\n \"hello\": 1\r\n })\r\n\r\n print (jdata)\r\n\r\n\r\n def __async_exec(self, jdata):\r\n try:\r\n ret = json.loads(self.loop.run_until_complete(self.__async_send_recieve(jdata)))\r\n except:\r\n ret = None\r\n\r\n return ret\r\n\r\n async def __async_send_recieve(self, jdata):\r\n async with self.aws:\r\n await self.aws.send(json.dumps(jdata))\r\n return await self.aws.receive()\r\n```\r\n\r\nSo I have the following in main.py:\r\n \r\n```\r\nfrom webclass import *\r\n\r\na = mtest('12341234')\r\nprint (a.sendtest())\r\n```\r\n\r\nThe problem is that it doesn't preserve the authorized session, so this is the output:\r\n\r\n```\r\nroot@ubupc1:/home/dinocob# python3 test.py \r\nasd@gmail.com\r\n{'error': {'message': 'Please log in.', 'code': 'AuthorizationRequired'}}\r\n```\r\n\r\nAs you see, the login call are working ok, but when calling and sending the `hello` in `sendtest` function, the session is not the same. \r\n\r\n - Where is destroyed the session? \r\n - How could I preserve it (without drastically\r\n modifying my class structure)?","author":{"url":"https://github.com/mllamazares","@type":"Person","name":"mllamazares"},"datePublished":"2017-02-09T17:18:59.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/491/asyncio/issues/491"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d53302bd-ca2a-0ae5-bc61-06dcfa43609f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ABAE:3301CD:470A179:5D0CDE5:696D4EF7 |
| html-safe-nonce | 12f2c697a673b3c9fffc918dce67e415e23a350272911e5c4abfcdbbdacaef7d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQkFFOjMzMDFDRDo0NzBBMTc5OjVEMENERTU6Njk2RDRFRjciLCJ2aXNpdG9yX2lkIjoiMjgwNjI4ODI1MzgwNTQ4MTcxOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e5dc6971961bf7af956bffb35a2dd780b7d4d38cecb1d2ddb1bb42385c5b080d |
| hovercard-subject-tag | issue:206570536 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/asyncio/491/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4175da14ebe382244c282fe8dc0e3cd86d0aeadb910cdc99965ab58cf6b240ce/python/asyncio/issues/491 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4175da14ebe382244c282fe8dc0e3cd86d0aeadb910cdc99965ab58cf6b240ce/python/asyncio/issues/491 |
| og:image:alt | 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 AsyncWebsoc... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mllamazares |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0944e430045f4fb5f7bd103e0660080acfe593174063212555773ca41c144a14 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/asyncio git https://github.com/python/asyncio.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 33739295 |
| octolytics-dimension-repository_nwo | python/asyncio |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 33739295 |
| octolytics-dimension-repository_network_root_nwo | python/asyncio |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 66b6e773801d28660016072069017cfcc851f54b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width