Compare commits
7 Commits
f62579ce90
...
c63ecf7781
Author | SHA1 | Date | |
---|---|---|---|
c63ecf7781 | |||
5d8affbb5f | |||
9e1e7c3377 | |||
a87893199c | |||
5c565bc62c | |||
d14fb10ddb | |||
3617494917 |
@ -44,9 +44,10 @@ class OMMClient2:
|
||||
:param id: User id
|
||||
"""
|
||||
|
||||
r = self.session.request(message.GetAccount(id))
|
||||
r = self.session.request(messages.GetAccount(id))
|
||||
r.raise_on_error()
|
||||
|
||||
if r.account is None:
|
||||
return None
|
||||
return r.account[0]
|
||||
|
||||
def ping(self):
|
||||
|
@ -22,7 +22,9 @@ class Connection:
|
||||
self._host = host
|
||||
self._port = port
|
||||
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self._recv_buffer = ""
|
||||
self._recv_buffer = b""
|
||||
|
||||
self._socket.settimeout(3)
|
||||
|
||||
def connect(self):
|
||||
"""
|
||||
@ -49,21 +51,26 @@ class Connection:
|
||||
|
||||
data = b""
|
||||
while True:
|
||||
new_data = self._socket.recv(65536)
|
||||
if new_data is not None:
|
||||
data += new_data
|
||||
else:
|
||||
try:
|
||||
new_data = self._socket.recv(1024)
|
||||
except TimeoutError:
|
||||
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
|
||||
return None
|
||||
|
||||
message, buffer = self._recv_buffer.split("\0", 1)
|
||||
message, buffer = self._recv_buffer.split(b"\0", 1)
|
||||
self._recv_buffer = buffer
|
||||
|
||||
return message
|
||||
return message.decode("utf-8")
|
||||
|
||||
def close(self):
|
||||
"""
|
||||
@ -80,6 +87,10 @@ class SSLConnection(Connection):
|
||||
"""
|
||||
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 port: Port of the OM Application XML ssl TCP port
|
||||
|
||||
|
@ -75,7 +75,7 @@ class Response:
|
||||
"""
|
||||
|
||||
if self.errCode is not None:
|
||||
raise exception_classes.get(self.errCode, OMResponseException)()
|
||||
raise exception_classes.get(self.errCode, OMResponseException)(response=self)
|
||||
|
||||
@property
|
||||
def seq(self):
|
||||
@ -98,6 +98,7 @@ class Response:
|
||||
return self.attrs.get("maxLen")
|
||||
|
||||
from .getaccount import GetAccount, GetAccountResp
|
||||
from .open import Open, OpenResp
|
||||
from .ping import Ping, PingResp
|
||||
|
||||
def construct(request):
|
||||
|
@ -24,4 +24,4 @@ class GetAccount(Request):
|
||||
class GetAccountResp(Response):
|
||||
@property
|
||||
def account(self):
|
||||
return self.attrs.get("account")
|
||||
return self.childs.get("account")
|
||||
|
@ -27,7 +27,7 @@ class Session:
|
||||
self.port = port
|
||||
self.connection_class = connection_class
|
||||
if self.connection_class is None:
|
||||
self.connection_class = connection.SSLConnection
|
||||
self.connection_class = connection.Connection
|
||||
|
||||
self._connection = None
|
||||
|
||||
@ -39,7 +39,7 @@ class Session:
|
||||
"""
|
||||
|
||||
while True:
|
||||
r = self.connection.recv()
|
||||
r = self._connection.recv()
|
||||
if r is not None:
|
||||
return r
|
||||
sleep(0.1)
|
||||
@ -58,11 +58,9 @@ class Session:
|
||||
self._connection.connect()
|
||||
|
||||
# Login
|
||||
self._connection.send(messages.Open(self.username, self.password))
|
||||
r = self.request(messages.Open(self.username, self.password))
|
||||
|
||||
res = self._wait_for_respose()
|
||||
|
||||
res.raise_on_error()
|
||||
r.raise_on_error()
|
||||
|
||||
def request(self, request):
|
||||
"""
|
||||
@ -77,7 +75,7 @@ class Session:
|
||||
"""
|
||||
|
||||
message = messages.construct(request)
|
||||
self.connection.send(message)
|
||||
self._connection.send(message)
|
||||
|
||||
res = self._wait_for_respose()
|
||||
return messages.parse(res)
|
||||
|
Loading…
Reference in New Issue
Block a user