Initial commit
This commit is contained in:
commit
39060babd4
4 changed files with 70 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
*.pyc
|
||||
pycache/
|
0
__init__.py
Normal file
0
__init__.py
Normal file
59
client.py
Normal file
59
client.py
Normal file
|
@ -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
|
||||
|
9
test.py
Normal file
9
test.py
Normal file
|
@ -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()))
|
Loading…
Reference in a new issue