commit 39060babd434f0a61095ac401ae4370e0e7b871a Author: Kumi Date: Tue Jul 19 11:25:29 2022 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..531ddf6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +pycache/ diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/client.py b/client.py new file mode 100644 index 0000000..318ebbf --- /dev/null +++ b/client.py @@ -0,0 +1,59 @@ +import asyncio + +from logging import Logger +from json import loads +from typing import Tuple + +logger = Logger("reportmonster-client") + + +class ReportMonsterResponse: + def __init__(self, raw): + self._raw = raw + + @property + def status(self) -> Tuple[int, str]: + return int(self._raw[:2]), self._raw.split(":")[0][3:] + + @property + def content(self) -> str | dict | list: + text = ":".join(self._raw.split(":")[1:]).strip() + + if self.status == 20: + return loads(text) + + return text + + @property + def error(self) -> bool: + return self.status >= 90 + + +class ReportMonsterClient: + def __init__(self, username, password, host="127.0.0.1", port=6789): + self.username = username + self.password = password + self.host = host + self.port = port + + self._socket_reader = None + self._socket_writer = None + + async def connect(self): + self._socket_reader, self._socket_writer = await asyncio.open_connection(self.host, self.port) + identification = await self.read() + return identification + + async def read(self): + content = await self._socket_reader.readuntil(b"> ") + return content.decode().rstrip().rstrip(">").rstrip() + + async def write(self, message): + await self._socket_writer.write(message.encode()) + + async def disconnect(self): + if self._socket_writer: + await self._socket_writer.close() + self._socket_writer = None + self._socket_reader = None + diff --git a/test.py b/test.py new file mode 100644 index 0000000..fdd6015 --- /dev/null +++ b/test.py @@ -0,0 +1,9 @@ +from client import ReportMonsterClient + +import asyncio + +async def test(): + client = ReportMonsterClient("test", "test") + return await client.connect() + +print(asyncio.run(test())) \ No newline at end of file