Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import asyncio # help i don't know asyncio
- import websockets
- import aiohttp
- import json
- import sys
- config = json.load(open('config.json'))
- async def get_endpoint_url():
- async with aiohttp.ClientSession() as session:
- async with session.get('https://sockchat.flashii.net/settings.json') as resp:
- settings = json.loads(await resp.text())
- print(settings['server'])
- return settings['server']
- def pack(*args):
- return str('\t'.join(str(a) for a in args))
- def unpack(rawmsg):
- return rawmsg.split('\t')
- async def receive(message):
- unp = unpack(message)
- if unp[0] == '0':
- return
- print(unp)
- async def receiver(websocket):
- while True:
- message = await websocket.recv()
- await receive(message)
- async def pinger(websocket):
- while True:
- await asyncio.sleep(25)
- await websocket.send(pack(0, config['uid']))
- async def chat():
- async with websockets.connect(
- await get_endpoint_url(),
- ssl=True
- ) as websocket:
- await websocket.send(pack(1, config['uid'], 'PASS:{}'.format(config['password'])))
- receiver_task = asyncio.ensure_future(receiver(websocket))
- ping_task = asyncio.ensure_future(pinger(websocket))
- done, pending = await asyncio.wait(
- [receiver_task, ping_task],
- return_when=asyncio.FIRST_COMPLETED,
- )
- for task in pending:
- task.cancel()
- def main():
- asyncio.get_event_loop().run_until_complete(chat())
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement