diff options
author | Stefan Wintermeyer <stefan.wintermeyer@amooma.de> | 2012-12-17 12:05:14 +0100 |
---|---|---|
committer | Stefan Wintermeyer <stefan.wintermeyer@amooma.de> | 2012-12-17 12:05:14 +0100 |
commit | eaad37485fe59d0306c37cc038dda6d210052910 (patch) | |
tree | 072c4b0e33d442528555b82c415f5e7a1712b2b0 /misc/mon_ami/tcp_server.py | |
parent | 3e706c2025ecc5523e81ad649639ef2ff75e7bac (diff) | |
parent | b80bd744ad873f6fc43018bc4bfb90677de167bd (diff) |
Merge branch 'develop'
Diffstat (limited to 'misc/mon_ami/tcp_server.py')
-rw-r--r-- | misc/mon_ami/tcp_server.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/misc/mon_ami/tcp_server.py b/misc/mon_ami/tcp_server.py new file mode 100644 index 0000000..5536282 --- /dev/null +++ b/misc/mon_ami/tcp_server.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +# MonAMI Asterisk Manger Interface Server +# TCP Server +# (c) AMOOMA GmbH 2012 + +import socket +from traceback import format_exc +from log import ldebug, linfo, lwarn, lerror, lcritic + +class TCPServer(): + + def __init__(self, address=None, port=None, timeout=1): + self.SOCKET_BACKLOG = 5 + self.port = port + self.address = address + self.socket_timeout = timeout + + def listen(self): + tcpsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + tcpsocket.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 ) + + ldebug('binding server to %s:%d, timeout: %d' % (self.address, self.port, self.socket_timeout), self) + + try: + tcpsocket.bind((self.address, self.port)) + except ValueError as exception: + lerror('server socket address error: %s - %s' % (exception, format_exc()), self) + return False + except socket.error as exception: + lerror('server socket error (%d): %s - %s' % (exception[0], exception[1], format_exc()), self) + return False + except: + lerror('general server socket error: %s' % format_exc(), self) + return False + + tcpsocket.listen(self.SOCKET_BACKLOG) + tcpsocket.settimeout(self.socket_timeout) + + return tcpsocket |