From a4fe47d589b848d5fe4980505f9f00cbe69d0032 Mon Sep 17 00:00:00 2001 From: manuel Date: Thu, 29 May 2014 15:58:19 +0200 Subject: sock.send might raise an exception (e.g. network unreachable) handle that --- python/webiopi/devices/sensor/icmp.py | 37 +++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/python/webiopi/devices/sensor/icmp.py b/python/webiopi/devices/sensor/icmp.py index 37028fa..be56a66 100644 --- a/python/webiopi/devices/sensor/icmp.py +++ b/python/webiopi/devices/sensor/icmp.py @@ -36,8 +36,11 @@ class ICMP(Duration): return Duration.__family__(self) def __getMilliseconds__(self): - sec = self.echo(self.ip, 1) - return sec * 1000 if sec > 0 else -1 + try: + sec = self.echo(self.ip, 1) + return sec * 1000 if sec > 0 else -1 + except IOError: + return -1 def close(self): pass @@ -117,16 +120,20 @@ class ICMP(Duration): try: socket.gethostbyname(dest_addr) except socket.gaierror: - return - - packet_id = int((id(timeout) * random.random()) % 65535) - packet = self.__create_packet__(packet_id) - while packet: - # The icmp protocol does not use a port, but the function - # below expects it, so we just give it a dummy port. - sent = sock.sendto(packet, (dest_addr, 1)) - packet = packet[sent:] - - delay = self.__response_handler__(sock, packet_id, time(), timeout) - sock.close() - return delay + return -1 + + try: + packet_id = int((id(timeout) * random.random()) % 65535) + packet = self.__create_packet__(packet_id) + while packet: + # The icmp protocol does not use a port, but the function + # below expects it, so we just give it a dummy port. + sent = sock.sendto(packet, (dest_addr, 1)) + packet = packet[sent:] + + delay = self.__response_handler__(sock, packet_id, time(), timeout) + return delay + except socket.error: + raise + finally: + sock.close() -- cgit v1.2.3