Created
June 28, 2021 15:47
-
-
Save mashony/ea8891ffdd6457d18a853f61503e8a2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Traceback (most recent call last): | |
File "cli.py", line 32, in <module> | |
loop.run_until_complete(main()) | |
File "python3.8/asyncio/base_events.py", line 616, in run_until_complete | |
return future.result() | |
File "cli.py", line 19, in main | |
async with session.ws_connect(URL, headers=headers) as ws: | |
File "python3.8/site-packages/aiohttp/client.py", line 1014, in __aenter__ | |
self._resp = await self._coro | |
File "python3.8/site-packages/aiohttp/client.py", line 723, in _ws_connect | |
resp = await self.request(method, url, | |
aiohttp.client_exceptions.WSServerHandshakeError: 400, message='Invalid response status', url=URL('ws://0.0.0.0:8080/ws') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import os | |
import aiohttp.web | |
HOST = os.getenv('HOST', '0.0.0.0') | |
PORT = int(os.getenv('PORT', 8080)) | |
async def websocket_handler(request): | |
print(dict(request.headers)) | |
ws = aiohttp.web.WebSocketResponse() | |
await ws.prepare(request) | |
async for msg in ws: | |
if msg.type == aiohttp.WSMsgType.TEXT: | |
if msg.data == 'close': | |
await ws.close() | |
else: | |
await ws.send_str(msg.data + '/answer') | |
elif msg.type == aiohttp.WSMsgType.ERROR: | |
print('ws connection closed with exception %s' % | |
ws.exception()) | |
print('websocket connection closed') | |
return ws | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
app = aiohttp.web.Application(loop=loop) | |
app.router.add_route('GET', '/ws', websocket_handler, name="ws") | |
aiohttp.web.run_app(app, host=HOST, port=PORT) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import os | |
import aiohttp | |
HOST = os.getenv('HOST', '0.0.0.0') | |
PORT = int(os.getenv('PORT', 8080)) | |
URL = f'ws://{HOST}:{PORT}/ws' | |
headers = { | |
"CustomHeader": "some-custom-str\n", | |
} | |
async def main(): | |
async with aiohttp.ClientSession() as session: | |
async with session.ws_connect(URL, headers=headers) as ws: | |
async for msg in ws: | |
if msg.type == aiohttp.WSMsgType.TEXT: | |
if msg.data == 'close cmd': | |
await ws.close() | |
break | |
else: | |
await ws.send_str(msg.data + '/answer') | |
elif msg.type == aiohttp.WSMsgType.ERROR: | |
break | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment