You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.3 KiB

import asyncio
import aiohttp
import zlib
import struct
import json
import settings
HEADER_LENGTH = 8
async def connect():
data = dict(
username=settings.UFP_USERNAME,
password=settings.UFP_PASSWORD,
rememberMe=True,
)
async with aiohttp.ClientSession() as session:
async with session.post(settings.UFP_ADDRESS + '/api/auth/login', json=data, ssl=False) as resp:
cookie = resp.cookies['TOKEN']
headers = {'cookie': cookie.key + '=' + cookie.value}
async with session.ws_connect(settings.UFP_ADDRESS + '/proxy/protect/ws/updates', headers=headers, ssl=False) as ws:
async for msg in ws:
packet_type, payload_format, deflated, unknown, payload_size = struct.unpack('!bbbbi', msg.data[0:HEADER_LENGTH])
action_start = HEADER_LENGTH
action_packet = zlib.decompress(msg.data[action_start:])
data_start = payload_size + 2*HEADER_LENGTH
data_packet = zlib.decompress(msg.data[data_start:])
yield json.loads(data_packet.decode())
async def test():
async for msg in connect():
print(msg)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(test())
loop.close()