From 6dc2f0403700c941085cfe5c622dec3ce1c201db Mon Sep 17 00:00:00 2001 From: berhsi Date: Sat, 14 Sep 2019 14:01:52 +0200 Subject: [PATCH] setstatus.py, statusd.py: timeouts added, change output messages --- setstatus.py | 9 ++++++--- statusd.py | 15 ++++++++++----- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/setstatus.py b/setstatus.py index e3a2b1c..2993689 100755 --- a/setstatus.py +++ b/setstatus.py @@ -1,7 +1,7 @@ #!/usr/bin/python3 # -*- coding: utf-8 -*- -# file: setstatus.py.py +# file: setstatus.py # date: 26.07.2019 # email: berhsi@web.de @@ -87,13 +87,16 @@ def main(*status): conn = context.wrap_socket(mySocket, server_side = False, \ server_hostname = SERVER_NAME) print('Connection wrapped with ssl.context') + conn.settimeout(5.0) except Exception as e: print('Context wrapper failed: [}'.format(e)) try: conn.connect((HOST, PORT)) - print('SSL established: {}'.format(conn.getpeercert())) + print('Connection established: {}'.format(conn.getpeercert())) + except socket.timeout: + print('Connection timeout') except Exception as e: - print('SSL handshake failed: {}'.format(e)) + print('Connection failed: {}'.format(e)) exit(1) try: print('Send new status: {}'.format(STATUS)) diff --git a/statusd.py b/statusd.py index 890ab7c..4d2f196 100755 --- a/statusd.py +++ b/statusd.py @@ -291,12 +291,17 @@ def main(): fromSocket.settimeout(3.0) try: conn = context.wrap_socket(fromSocket, server_side = True) + conn.settimeout(3.0) # display_peercert(conn.getpeercert()) - logging.info('SSL connection established') - logging.info('commonName: {}'.format(conn.getpeercert()['subject'][5][0][1])) - logging.info('serialNumber: {}'.format(conn.getpeercert()['serialNumber'])) + logging.debug('Connection established') + logging.debug('Peer certificate commonName: {}'.format \ + (conn.getpeercert()['subject'][5][0][1])) + logging.debug('Peer certificate serialNumber: {}'.format \ + (conn.getpeercert()['serialNumber'])) + except socket.timeout: + logging.error('Socket timeout') except Exception as e: - logging.error('SSL handshake failed: {}'.format(e)) + logging.error('Connection failed: {}'.format(e)) raw_data = conn.recv(1) if receive_buffer_is_valid(raw_data) == True: if change_status(raw_data, CONFIG['API']) == True: @@ -309,7 +314,7 @@ def main(): conn.send(b'\x03') # recive_handle returns false: else: - logging.info('Inalid argument recived: {}'.format(raw_data)) + logging.info('Invalid argument recived: {}'.format(raw_data)) logging.debug('Send {} back'.format(b'\x03')) if conn: conn.send(b'\x03')