Compare commits

...

7 Commits

5 changed files with 31 additions and 20 deletions

View File

@ -44,9 +44,10 @@ class OMMClient2:
:param id: User id :param id: User id
""" """
r = self.session.request(message.GetAccount(id)) r = self.session.request(messages.GetAccount(id))
r.raise_on_error() r.raise_on_error()
if r.account is None:
return None
return r.account[0] return r.account[0]
def ping(self): def ping(self):

View File

@ -22,7 +22,9 @@ class Connection:
self._host = host self._host = host
self._port = port self._port = port
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._recv_buffer = "" self._recv_buffer = b""
self._socket.settimeout(3)
def connect(self): def connect(self):
""" """
@ -49,21 +51,26 @@ class Connection:
data = b"" data = b""
while True: while True:
new_data = self._socket.recv(65536) try:
if new_data is not None: new_data = self._socket.recv(1024)
data += new_data except TimeoutError:
else:
break break
self._recv_buffer += data.decode("utf-8")
if "\0" not in self._recv_buffer: data += new_data
if b"\0" in new_data:
break
self._recv_buffer += data
if b"\0" not in self._recv_buffer:
# no new messages # no new messages
return None return None
message, buffer = self._recv_buffer.split("\0", 1) message, buffer = self._recv_buffer.split(b"\0", 1)
self._recv_buffer = buffer self._recv_buffer = buffer
return message return message.decode("utf-8")
def close(self): def close(self):
""" """
@ -80,6 +87,10 @@ class SSLConnection(Connection):
""" """
Establishes a secure connection to the OM Application XML Interface Establishes a secure connection to the OM Application XML Interface
Please not that this class might be useless on your system since new
versions of OpenSSL don't ship with TLVv1.2 or lower anymore which are
the protocols supported by OMM.
:param host: Hostname or IP address of OMM :param host: Hostname or IP address of OMM
:param port: Port of the OM Application XML ssl TCP port :param port: Port of the OM Application XML ssl TCP port

View File

@ -75,7 +75,7 @@ class Response:
""" """
if self.errCode is not None: if self.errCode is not None:
raise exception_classes.get(self.errCode, OMResponseException)() raise exception_classes.get(self.errCode, OMResponseException)(response=self)
@property @property
def seq(self): def seq(self):
@ -98,6 +98,7 @@ class Response:
return self.attrs.get("maxLen") return self.attrs.get("maxLen")
from .getaccount import GetAccount, GetAccountResp from .getaccount import GetAccount, GetAccountResp
from .open import Open, OpenResp
from .ping import Ping, PingResp from .ping import Ping, PingResp
def construct(request): def construct(request):

View File

@ -24,4 +24,4 @@ class GetAccount(Request):
class GetAccountResp(Response): class GetAccountResp(Response):
@property @property
def account(self): def account(self):
return self.attrs.get("account") return self.childs.get("account")

View File

@ -27,7 +27,7 @@ class Session:
self.port = port self.port = port
self.connection_class = connection_class self.connection_class = connection_class
if self.connection_class is None: if self.connection_class is None:
self.connection_class = connection.SSLConnection self.connection_class = connection.Connection
self._connection = None self._connection = None
@ -39,7 +39,7 @@ class Session:
""" """
while True: while True:
r = self.connection.recv() r = self._connection.recv()
if r is not None: if r is not None:
return r return r
sleep(0.1) sleep(0.1)
@ -58,11 +58,9 @@ class Session:
self._connection.connect() self._connection.connect()
# Login # Login
self._connection.send(messages.Open(self.username, self.password)) r = self.request(messages.Open(self.username, self.password))
res = self._wait_for_respose() r.raise_on_error()
res.raise_on_error()
def request(self, request): def request(self, request):
""" """
@ -77,7 +75,7 @@ class Session:
""" """
message = messages.construct(request) message = messages.construct(request)
self.connection.send(message) self._connection.send(message)
res = self._wait_for_respose() res = self._wait_for_respose()
return messages.parse(res) return messages.parse(res)