diff options
| -rw-r--r-- | python/webiopi/devices/sensor/icmp.py | 37 |
1 files 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): | |||
| 36 | return Duration.__family__(self) | 36 | return Duration.__family__(self) |
| 37 | 37 | ||
| 38 | def __getMilliseconds__(self): | 38 | def __getMilliseconds__(self): |
| 39 | sec = self.echo(self.ip, 1) | 39 | try: |
| 40 | return sec * 1000 if sec > 0 else -1 | 40 | sec = self.echo(self.ip, 1) |
| 41 | return sec * 1000 if sec > 0 else -1 | ||
| 42 | except IOError: | ||
| 43 | return -1 | ||
| 41 | 44 | ||
| 42 | def close(self): | 45 | def close(self): |
| 43 | pass | 46 | pass |
| @@ -117,16 +120,20 @@ class ICMP(Duration): | |||
| 117 | try: | 120 | try: |
| 118 | socket.gethostbyname(dest_addr) | 121 | socket.gethostbyname(dest_addr) |
| 119 | except socket.gaierror: | 122 | except socket.gaierror: |
| 120 | return | 123 | return -1 |
| 121 | 124 | ||
| 122 | packet_id = int((id(timeout) * random.random()) % 65535) | 125 | try: |
| 123 | packet = self.__create_packet__(packet_id) | 126 | packet_id = int((id(timeout) * random.random()) % 65535) |
| 124 | while packet: | 127 | packet = self.__create_packet__(packet_id) |
| 125 | # The icmp protocol does not use a port, but the function | 128 | while packet: |
| 126 | # below expects it, so we just give it a dummy port. | 129 | # The icmp protocol does not use a port, but the function |
| 127 | sent = sock.sendto(packet, (dest_addr, 1)) | 130 | # below expects it, so we just give it a dummy port. |
| 128 | packet = packet[sent:] | 131 | sent = sock.sendto(packet, (dest_addr, 1)) |
| 129 | 132 | packet = packet[sent:] | |
| 130 | delay = self.__response_handler__(sock, packet_id, time(), timeout) | 133 | |
| 131 | sock.close() | 134 | delay = self.__response_handler__(sock, packet_id, time(), timeout) |
| 132 | return delay | 135 | return delay |
| 136 | except socket.error: | ||
| 137 | raise | ||
| 138 | finally: | ||
| 139 | sock.close() | ||
