From a4b613f9b57eb543bfefe64192d15d8af36ece35 Mon Sep 17 00:00:00 2001 From: Mark Jessop Date: Mon, 8 Oct 2018 15:44:51 +1030 Subject: [PATCH] Send UDP broadcast messages to localhost if a network error is encountered. --- auto_rx/autorx/ozimux.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/auto_rx/autorx/ozimux.py b/auto_rx/autorx/ozimux.py index 3657b26..15d8909 100644 --- a/auto_rx/autorx/ozimux.py +++ b/auto_rx/autorx/ozimux.py @@ -87,7 +87,15 @@ class OziUploader(object): _ozisock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) except: pass - _ozisock.sendto(_sentence.encode('ascii'),('',self.ozimux_port)) + + try: + _ozisock.sendto(_sentence.encode('ascii'),('',self.ozimux_port)) + # Catch any socket errors, that may occur when attempting to send to a broadcast address + # when there is no network connected. In this case, re-try and send to localhost instead. + except socket.error as e: + self.log_debug("Send to broadcast address failed, sending to localhost instead.") + _ozisock.sendto(_sentence.encode('ascii'),('127.0.0.1',self.ozimux_port)) + _ozisock.close() except Exception as e: @@ -146,7 +154,16 @@ class OziUploader(object): except: pass - _s.sendto(json.dumps(packet).encode('ascii'), ('', self.payload_summary_port)) + try: + _s.sendto(json.dumps(packet).encode('ascii'), ('', self.payload_summary_port)) + # Catch any socket errors, that may occur when attempting to send to a broadcast address + # when there is no network connected. In this case, re-try and send to localhost instead. + except socket.error as e: + self.log_debug("Send to broadcast address failed, sending to localhost instead.") + _s.sendto(json.dumps(packet).encode('ascii'), ('127.0.0.1', self.payload_summary_port)) + + _s.close() + except Exception as e: self.log_error("Error sending Payload Summary: %s" % str(e))