Merge branch 'master' into PythonModule
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Marcel Nijenhof
2020-09-04 18:01:05 +02:00

View File

@@ -28,7 +28,8 @@ Support for:
def __init__(self, user=None, password=None, def __init__(self, user=None, password=None,
host="sip-lmw.rws.nl", port=443, meetnet="LMW", ssl = True, host="sip-lmw.rws.nl", port=443, meetnet="LMW", ssl = True,
check_ssl = True, timeout = 10, log = None, cleartelnet = False): check_ssl = True, timeout = 10, log = None, cleartelnet = False,
reconnecttime=540):
"""LmwSip(user, password, [host], [port], [meetnet], [ssl], [check_ssl], [timeout], [log]) """LmwSip(user, password, [host], [port], [meetnet], [ssl], [check_ssl], [timeout], [log])
user(optinal): Lmw user name user(optinal): Lmw user name
@@ -37,26 +38,30 @@ host(optional): Default sip-lmw.rws.nl
port(optional): Default 443 port(optional): Default 443
meetnet(optional): Default LMW meetnet(optional): Default LMW
ssl(optional): Default true ssl(optional): Default true
check_ssl(optional): true check_ssl(optional): Default true
timeout(optional): 10 timeout(optional): Default 10
log(optional): None log(optional): Default None
cleartelnet: False cleartelnet(optional): Default False
reconnecttime(optional): Default 540
Opens the connection and logs in Opens the connection and logs in.
""" """
self.user = user self.user = user
self.password = password self.password = password
self.host = host self.host = host
self.port = port self.port = port
self.meetnet = meetnet self.meetnet = meetnet
self.ssl = ssl self.ssl = ssl
self.check_ssl = check_ssl self.check_ssl = check_ssl
self.timeout = timeout self.timeout = timeout
self._socket = None self.cleartelnet = cleartelnet
self.cleartelnet = cleartelnet self.reconnecttime = reconnecttime
self._connecttime = time.time()
self._socket = None
if (log != None): if (log != None):
self.log = log self.log = log
self.log.debug("LmwSip.init") self.log.debug("LmwSip.init(%s, **********, %s, %s, %s, %s, %s, %s, %s, %s)" %
(user, host, port, meetnet, ssl, check_ssl, timeout, cleartelnet, reconnecttime))
else: else:
try: try:
self.log = logging.getLogger("lmwsip") self.log = logging.getLogger("lmwsip")
@@ -67,6 +72,8 @@ Opens the connection and logs in
self.connect() self.connect()
if (self.user != None): if (self.user != None):
self.login() self.login()
else:
self.reconnecttime = 0
def lasttime(self, parameter): def lasttime(self, parameter):
# #
@@ -106,6 +113,7 @@ connects to lmw with tcp using the values of the object creation.
""" """
try: try:
self._tcp = socket.create_connection((self.host, self.port)) self._tcp = socket.create_connection((self.host, self.port))
self._connecttime = time.time()
except Exception as e: except Exception as e:
self.log.error("LmwSip.connect(%s, %s) failed: %s", self.log.error("LmwSip.connect(%s, %s) failed: %s",
self.host, self.port, e) self.host, self.port, e)
@@ -134,11 +142,31 @@ connects to lmw with tcp using the values of the object creation.
pass pass
self._socket = None self._socket = None
def reconnectcheck(self):
"""Checks if the connection is longer open than the reconnect time.
After this time a logout is sent and a new connection is created.
This prevents the 10 minute server timeout"""
if self.reconnecttime > 0:
ct = time.time() - self._connecttime
if ct > self.reconnecttime:
self.log.debug("LmwSip.reconnectcheck: reconnect after %i seconds" % ct)
#
# Disable check for the reconnect
#
self.reconnecttime = - self.reconnecttime
self.logout()
time.sleep(1)
self.connect()
self.login()
self.reconnecttime = - self.reconnecttime
def send(self, sipcmd): def send(self, sipcmd):
"""send(sipcmd) """send(sipcmd)
send a sip command to the server send a sip command to the server
""" """
self.reconnectcheck()
if self._socket != None: if self._socket != None:
try: try:
logcmd = sipcmd.strip('\r') logcmd = sipcmd.strip('\r')
@@ -147,12 +175,22 @@ send a sip command to the server
self.log.debug("LmwSip.send(%s)" % logcmd) self.log.debug("LmwSip.send(%s)" % logcmd)
self._socket.sendall(sipcmd.encode('ascii')) self._socket.sendall(sipcmd.encode('ascii'))
except Exception as e: except Exception as e:
self.log.error("LmwSip.send(%s) failed: %s", sipcmd, e) self.log.error("LmwSip.send(%s) failed: %s" % (sipcmd, e))
self.closesocket() self.closesocket()
raise LmwSipConnectError("LmwSip.send: Socket connection lost") raise LmwSipConnectError("LmwSip.send: Socket connection lost")
else: else:
self.log.warning("LmwSip.send: No connection") self.log.warning("LmwSip.send: No connection")
def telnetheader(self, header):
a = b'\xff\xfd\x01\xff\xfd\x03\xff\xfd\x00\xff\xfc\x01\xff\xfb\x00'
self.log.debug("LmwSip.telnetheader(%s) --> %s" % (header, a))
try:
self._socket.sendall(a)
except Exception as e:
self.log.error("LmwSip.telnetheader(%s) --> %s failed: %s" % (header, a, e))
self.closesocket()
raise LmwSipConnectError("LmwSip.telnetheader: Socket connection lost")
def recv(self): def recv(self):
"""recv() """recv()
@@ -160,26 +198,29 @@ recieve a answer from the sip server
""" """
bytebuf=b'' bytebuf=b''
stringbuf="" stringbuf=""
while self._socket != None and re.search("\r$", bytebuf.decode('utf-8')) == None: while (self._socket != None) and (stringbuf.find("\r") == -1):
try: try:
self.log.debug("LmwSip.recv: Waiting for data"); self.log.debug("LmwSip.recv: %s: Waiting for data" % self.cleartelnet);
bytebuf = self._socket.recv(4096) bytebuf = self._socket.recv(4096)
self.log.debug("recv: bytebuf: %s" % bytebuf)
if self.cleartelnet: if self.cleartelnet:
while bytebuf[0] == '\xff': if bytebuf[0] == 255:
self.log.info("SipLmw.recv: telnet data: %s", bytebuf) bytebuf = b''
bytebuf = self._socket.recv(4096)
except Exception as e: except Exception as e:
self.log.error("SipLmw.recv: socket timeout: %s", e) self.log.error("SipLmw.recv: socket timeout: %s", e)
self.closesocket() self.closesocket()
raise LmwSipConnectError("LmwSip.recv: Socket timeout") raise LmwSipConnectError("LmwSip.recv: No data recieved")
try: try:
stringbuf += bytebuf.decode('utf-8') stringbuf += bytebuf.decode('utf-8')
self.log.debug("recv: stringbuf: %s" % stringbuf)
except Exception as e: except Exception as e:
self.log.error("SipLmw.recv: decode error: %s", e) self.log.error("SipLmw.recv: decode error: %s", e)
self.closesocket() self.closesocket()
raise LmwSipDecodeError("LmwSip.recv: decode error", bytebuf) raise LmwSipDecodeError("LmwSip.recv: decode error", bytebuf)
if self._socket == None: if self._socket == None:
self.log.warn("LmwSip.recv: No connection") self.log.warn("LmwSip.recv: No connection")
elif len(stringbuf) == 0:
self.log.warn("LmwSip.recv: No data")
elif stringbuf[0] != '!': elif stringbuf[0] != '!':
self.log.warn("LmwSip.recv: Sip error: %s" % stringbuf.strip('\r')) self.log.warn("LmwSip.recv: Sip error: %s" % stringbuf.strip('\r'))
else: else: