Advertisement
szymonwalle

sockchat sessionkeeper

Sep 28th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import asyncio # help i don't know asyncio
  3. import websockets
  4. import aiohttp
  5. import json
  6. import sys
  7.  
  8. config = json.load(open('config.json'))
  9.  
  10. async def get_endpoint_url():
  11.     async with aiohttp.ClientSession() as session:
  12.         async with session.get('https://sockchat.flashii.net/settings.json') as resp:
  13.             settings = json.loads(await resp.text())
  14.             print(settings['server'])
  15.             return settings['server']
  16.  
  17. def pack(*args):
  18.     return str('\t'.join(str(a) for a in args))
  19.  
  20. def unpack(rawmsg):
  21.     return rawmsg.split('\t')
  22.  
  23. async def receive(message):
  24.     unp = unpack(message)
  25.     if unp[0] == '0':
  26.         return
  27.     print(unp)
  28.  
  29. async def receiver(websocket):
  30.     while True:
  31.         message = await websocket.recv()
  32.         await receive(message)
  33.  
  34. async def pinger(websocket):
  35.     while True:
  36.         await asyncio.sleep(25)
  37.         await websocket.send(pack(0, config['uid']))
  38.  
  39. async def chat():
  40.     async with websockets.connect(
  41.             await get_endpoint_url(),
  42.             ssl=True
  43.         ) as websocket:
  44.         await websocket.send(pack(1, config['uid'], 'PASS:{}'.format(config['password'])))
  45.         receiver_task = asyncio.ensure_future(receiver(websocket))
  46.         ping_task = asyncio.ensure_future(pinger(websocket))
  47.         done, pending = await asyncio.wait(
  48.             [receiver_task, ping_task],
  49.             return_when=asyncio.FIRST_COMPLETED,
  50.         )
  51.         for task in pending:
  52.             task.cancel()
  53.        
  54. def main():
  55.     asyncio.get_event_loop().run_until_complete(chat())
  56.  
  57. if __name__ == '__main__':
  58.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement