Merge branch 'master' into PythonModule
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -28,7 +28,8 @@ Support for:
|
||||
|
||||
def __init__(self, user=None, password=None,
|
||||
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])
|
||||
|
||||
user(optinal): Lmw user name
|
||||
@@ -37,12 +38,13 @@ host(optional): Default sip-lmw.rws.nl
|
||||
port(optional): Default 443
|
||||
meetnet(optional): Default LMW
|
||||
ssl(optional): Default true
|
||||
check_ssl(optional): true
|
||||
timeout(optional): 10
|
||||
log(optional): None
|
||||
cleartelnet: False
|
||||
check_ssl(optional): Default true
|
||||
timeout(optional): Default 10
|
||||
log(optional): Default None
|
||||
cleartelnet(optional): Default False
|
||||
reconnecttime(optional): Default 540
|
||||
|
||||
Opens the connection and logs in
|
||||
Opens the connection and logs in.
|
||||
"""
|
||||
self.user = user
|
||||
self.password = password
|
||||
@@ -52,11 +54,14 @@ Opens the connection and logs in
|
||||
self.ssl = ssl
|
||||
self.check_ssl = check_ssl
|
||||
self.timeout = timeout
|
||||
self._socket = None
|
||||
self.cleartelnet = cleartelnet
|
||||
self.reconnecttime = reconnecttime
|
||||
self._connecttime = time.time()
|
||||
self._socket = None
|
||||
if (log != None):
|
||||
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:
|
||||
try:
|
||||
self.log = logging.getLogger("lmwsip")
|
||||
@@ -67,6 +72,8 @@ Opens the connection and logs in
|
||||
self.connect()
|
||||
if (self.user != None):
|
||||
self.login()
|
||||
else:
|
||||
self.reconnecttime = 0
|
||||
|
||||
def lasttime(self, parameter):
|
||||
#
|
||||
@@ -106,6 +113,7 @@ connects to lmw with tcp using the values of the object creation.
|
||||
"""
|
||||
try:
|
||||
self._tcp = socket.create_connection((self.host, self.port))
|
||||
self._connecttime = time.time()
|
||||
except Exception as e:
|
||||
self.log.error("LmwSip.connect(%s, %s) failed: %s",
|
||||
self.host, self.port, e)
|
||||
@@ -134,11 +142,31 @@ connects to lmw with tcp using the values of the object creation.
|
||||
pass
|
||||
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):
|
||||
"""send(sipcmd)
|
||||
|
||||
send a sip command to the server
|
||||
"""
|
||||
self.reconnectcheck()
|
||||
if self._socket != None:
|
||||
try:
|
||||
logcmd = sipcmd.strip('\r')
|
||||
@@ -147,12 +175,22 @@ send a sip command to the server
|
||||
self.log.debug("LmwSip.send(%s)" % logcmd)
|
||||
self._socket.sendall(sipcmd.encode('ascii'))
|
||||
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()
|
||||
raise LmwSipConnectError("LmwSip.send: Socket connection lost")
|
||||
else:
|
||||
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):
|
||||
"""recv()
|
||||
|
||||
@@ -160,26 +198,29 @@ recieve a answer from the sip server
|
||||
"""
|
||||
bytebuf=b''
|
||||
stringbuf=""
|
||||
while self._socket != None and re.search("\r$", bytebuf.decode('utf-8')) == None:
|
||||
while (self._socket != None) and (stringbuf.find("\r") == -1):
|
||||
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)
|
||||
self.log.debug("recv: bytebuf: %s" % bytebuf)
|
||||
if self.cleartelnet:
|
||||
while bytebuf[0] == '\xff':
|
||||
self.log.info("SipLmw.recv: telnet data: %s", bytebuf)
|
||||
bytebuf = self._socket.recv(4096)
|
||||
if bytebuf[0] == 255:
|
||||
bytebuf = b''
|
||||
except Exception as e:
|
||||
self.log.error("SipLmw.recv: socket timeout: %s", e)
|
||||
self.closesocket()
|
||||
raise LmwSipConnectError("LmwSip.recv: Socket timeout")
|
||||
raise LmwSipConnectError("LmwSip.recv: No data recieved")
|
||||
try:
|
||||
stringbuf += bytebuf.decode('utf-8')
|
||||
self.log.debug("recv: stringbuf: %s" % stringbuf)
|
||||
except Exception as e:
|
||||
self.log.error("SipLmw.recv: decode error: %s", e)
|
||||
self.closesocket()
|
||||
raise LmwSipDecodeError("LmwSip.recv: decode error", bytebuf)
|
||||
if self._socket == None:
|
||||
self.log.warn("LmwSip.recv: No connection")
|
||||
elif len(stringbuf) == 0:
|
||||
self.log.warn("LmwSip.recv: No data")
|
||||
elif stringbuf[0] != '!':
|
||||
self.log.warn("LmwSip.recv: Sip error: %s" % stringbuf.strip('\r'))
|
||||
else:
|
||||
|
Reference in New Issue
Block a user