Issue 1, 3: Integratie logger & verbetering fout detectie
This commit is contained in:
102
lmwsip.py
102
lmwsip.py
@@ -4,8 +4,10 @@ See: LmwSip"""
|
|||||||
|
|
||||||
import socket
|
import socket
|
||||||
import ssl
|
import ssl
|
||||||
|
import select
|
||||||
import time
|
import time
|
||||||
import re
|
import re
|
||||||
|
import logging
|
||||||
|
|
||||||
class LmwSip:
|
class LmwSip:
|
||||||
"""Class to connect to the LMW Standard Interface prototcol (sip)
|
"""Class to connect to the LMW Standard Interface prototcol (sip)
|
||||||
@@ -23,9 +25,9 @@ Support for:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, user=None, password=None,
|
def __init__(self, user=None, password=None,
|
||||||
host="sip-lmw.rws.nl", port=443,
|
host="sip-lmw.rws.nl", port=443, meetnet="LMW", ssl = True,
|
||||||
meetnet="LMW", ssl = True, check_ssl = True):
|
check_ssl = True, timeout = 10, log = None):
|
||||||
"""LmwSip(user, password, [host], [port], [meetnet], [ssl], [check_ssl])
|
"""LmwSip(user, password, [host], [port], [meetnet], [ssl], [check_ssl], [timeout], [log])
|
||||||
|
|
||||||
user(optinal): Lmw user name
|
user(optinal): Lmw user name
|
||||||
password(optional): Lmw password
|
password(optional): Lmw password
|
||||||
@@ -34,6 +36,8 @@ 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): true
|
||||||
|
timeout(optional): 10
|
||||||
|
log(optional): None
|
||||||
|
|
||||||
Opens the connection and logs in
|
Opens the connection and logs in
|
||||||
"""
|
"""
|
||||||
@@ -44,6 +48,17 @@ Opens the connection and logs in
|
|||||||
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._socket = None
|
||||||
|
if (log != None):
|
||||||
|
self.log = log
|
||||||
|
self.log.debug("LmwSip.init")
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
self.log = logging.getLogger("lmwsip")
|
||||||
|
self.log.debug("LmwSip.init: Start log")
|
||||||
|
except Exception as e:
|
||||||
|
print("Logger failed: %s" % e)
|
||||||
if (self.host != None):
|
if (self.host != None):
|
||||||
self.connect()
|
self.connect()
|
||||||
if (self.user != None):
|
if (self.user != None):
|
||||||
@@ -85,22 +100,51 @@ Opens the connection and logs in
|
|||||||
|
|
||||||
connects to lmw with tcp using the values of the object creation.
|
connects to lmw with tcp using the values of the object creation.
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
self._tcp = socket.create_connection((self.host, self.port))
|
self._tcp = socket.create_connection((self.host, self.port))
|
||||||
|
except Exception as e:
|
||||||
|
self.log.error("LmwSip.connect(%s, %s) failed: %s",
|
||||||
|
self.host, self.port, e)
|
||||||
|
raise LmwSipConnectError("LmwSip.connect: Socket create failed")
|
||||||
if (self.ssl):
|
if (self.ssl):
|
||||||
|
try:
|
||||||
self._context = ssl.create_default_context()
|
self._context = ssl.create_default_context()
|
||||||
self._context.check_hostname = self.check_ssl
|
self._context.check_hostname = self.check_ssl
|
||||||
self._ssl = self._context.wrap_socket(self._tcp,
|
self._ssl = self._context.wrap_socket(self._tcp,
|
||||||
server_hostname=self.host)
|
server_hostname=self.host)
|
||||||
self._socket = self._ssl
|
self._socket = self._ssl
|
||||||
|
except Exception as e:
|
||||||
|
self.log.error("LmwSip.connect setup ssl failed:\n%s", e)
|
||||||
|
raise LmwSipConnectError("LmwSip.connect: setup ssl failed")
|
||||||
else:
|
else:
|
||||||
self._socket = self._tcp
|
self._socket = self._tcp
|
||||||
|
self._socket.settimeout(self.timeout)
|
||||||
|
|
||||||
|
def closesocket(self):
|
||||||
|
"""Closes the socket and set the socket to None. Doesn't logout"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.log.debug("LmwSip.closesocket")
|
||||||
|
self._socket.close()
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
self._socket = None
|
||||||
|
|
||||||
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
|
||||||
"""
|
"""
|
||||||
|
if self._socket != None:
|
||||||
|
try:
|
||||||
|
self.log.debug("LmwSip.send(%s)" % sipcmd)
|
||||||
self._socket.sendall(sipcmd.encode('ascii'))
|
self._socket.sendall(sipcmd.encode('ascii'))
|
||||||
|
except Exception as e:
|
||||||
|
self.log.error("LmwSip.send(%s) failed: %s", sipcmd, e)
|
||||||
|
self.closesocket()
|
||||||
|
raise LmwSipConnectError("LmwSip.send: Socket connection lost")
|
||||||
|
else:
|
||||||
|
self.log.warn("LmwSip.send: No connection")
|
||||||
|
|
||||||
def recv(self):
|
def recv(self):
|
||||||
"""recv()
|
"""recv()
|
||||||
@@ -108,8 +152,26 @@ send a sip command to the server
|
|||||||
recieve a answer from the sip server
|
recieve a answer from the sip server
|
||||||
"""
|
"""
|
||||||
buf=""
|
buf=""
|
||||||
while re.search("\r$", buf) == None:
|
while self._socket != None and re.search("\r$", buf) == None:
|
||||||
buf += self._socket.recv(4096).decode('utf-8')
|
try:
|
||||||
|
self.log.debug("LmwSip.recv: Waiting for data");
|
||||||
|
b = self._socket.recv(4096).decode('utf-8')
|
||||||
|
if (b == ""):
|
||||||
|
self.log.error("SipLmw.recv: socket closed")
|
||||||
|
self.closesocket()
|
||||||
|
raise LmwSipConnectError("LmwSip.recv: Socket close")
|
||||||
|
else:
|
||||||
|
buf += b
|
||||||
|
except Exception as e:
|
||||||
|
self.log.error("SipLmw.recv: socket timeout: %s", e)
|
||||||
|
self.closesocket()
|
||||||
|
raise LmwSipConnectError("LmwSip.recv: Socket timeout")
|
||||||
|
if self._socket == None:
|
||||||
|
self.log.warn("LmwSip.recv: No connection")
|
||||||
|
elif buf[0] != '!':
|
||||||
|
self.log.warn("LmwSip.recv: Sip error: %s" % buf)
|
||||||
|
else:
|
||||||
|
self.log.debug("LmwSip.recv: result: %s" % buf)
|
||||||
return(buf)
|
return(buf)
|
||||||
|
|
||||||
def login(self):
|
def login(self):
|
||||||
@@ -135,7 +197,7 @@ Raises a LmwCmdWarn of failure
|
|||||||
self.send(ti)
|
self.send(ti)
|
||||||
d = self.recv()
|
d = self.recv()
|
||||||
if (d('ascii')[0] != '!'):
|
if (d('ascii')[0] != '!'):
|
||||||
raise LmwCmdWarn("TI " + self.meetnet +":" + d)
|
raise LmwCmdWarn(ti, d)
|
||||||
return (d[2:-1])
|
return (d[2:-1])
|
||||||
|
|
||||||
def cmd(self, process, location, parameter, time_delta, day,
|
def cmd(self, process, location, parameter, time_delta, day,
|
||||||
@@ -170,12 +232,12 @@ Returns:
|
|||||||
self.send(cmdstr)
|
self.send(cmdstr)
|
||||||
d = self.recv()
|
d = self.recv()
|
||||||
if (d[0] != '!'):
|
if (d[0] != '!'):
|
||||||
raise LmwCmdWarn(d)
|
raise LmwCmdWarn(cmdstr, d)
|
||||||
return (d[2:-1])
|
return (d[2:-1])
|
||||||
|
|
||||||
def value(self, process, location, parameter, day = None,
|
def value(self, process, location, parameter, day = None,
|
||||||
time_of_day = None):
|
time_of_day = None):
|
||||||
"""value(process, location, parameter, [day], [time_of_day], [cmd_type="DATA"]):
|
"""value(process, location, parameter, [day], [time_of_day]):
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
process: <WN|VW|AS>
|
process: <WN|VW|AS>
|
||||||
@@ -215,9 +277,33 @@ Returns a single string value or None
|
|||||||
Logs of
|
Logs of
|
||||||
"""
|
"""
|
||||||
self.send("LO\r")
|
self.send("LO\r")
|
||||||
|
self.closesocket()
|
||||||
|
|
||||||
|
|
||||||
|
class LmwSipConnectError(Exception):
|
||||||
|
"""Connection exceptions for LmwSip"""
|
||||||
|
|
||||||
|
def __init__(self, message):
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return(self.message)
|
||||||
|
|
||||||
class LmwLoginFailure(Exception):
|
class LmwLoginFailure(Exception):
|
||||||
"""Exception from LmwSip on login failure"""
|
"""Exception from LmwSip on login failure"""
|
||||||
|
|
||||||
|
def __init__(self, user, message):
|
||||||
|
self.user = user
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return("Login with user %s failed: %s", self.user, self.message)
|
||||||
|
|
||||||
class LmwCmdWarn(Warning):
|
class LmwCmdWarn(Warning):
|
||||||
"""Exception fro LmwSip on a cmd"""
|
"""Exception fro LmwSip on a cmd"""
|
||||||
|
def __init__(self, cmd, message):
|
||||||
|
self.cmd = cmd
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return("Cmd %s failed: %s", self.cmd, self.message)
|
||||||
|
7
siprun
7
siprun
@@ -46,7 +46,11 @@ def main():
|
|||||||
print("Set host and port")
|
print("Set host and port")
|
||||||
usage()
|
usage()
|
||||||
sys.exit(3)
|
sys.exit(3)
|
||||||
|
try:
|
||||||
lmwsip = LmwSip(ssl=ssl, host=host, port=port)
|
lmwsip = LmwSip(ssl=ssl, host=host, port=port)
|
||||||
|
except Exception as e:
|
||||||
|
print("Connect to lmw failed: %s" % e)
|
||||||
|
exit(1)
|
||||||
if (date == None or time == None):
|
if (date == None or time == None):
|
||||||
# We assume a 10 minut interval so we use H10
|
# We assume a 10 minut interval so we use H10
|
||||||
r = lmwsip.lasttime("H10")
|
r = lmwsip.lasttime("H10")
|
||||||
@@ -61,8 +65,11 @@ def main():
|
|||||||
cmd = cmd.replace('{TIME}', time)
|
cmd = cmd.replace('{TIME}', time)
|
||||||
cmd = cmd.replace('\n', '\r')
|
cmd = cmd.replace('\n', '\r')
|
||||||
print("> [%s]" % (cmd.strip('\r')))
|
print("> [%s]" % (cmd.strip('\r')))
|
||||||
|
try:
|
||||||
lmwsip.send(cmd)
|
lmwsip.send(cmd)
|
||||||
print("< [%s]" % (lmwsip.recv().strip('\r')))
|
print("< [%s]" % (lmwsip.recv().strip('\r')))
|
||||||
|
except:
|
||||||
|
pass
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Reference in New Issue
Block a user