From aa8fe516843e7a520b231aceb5814a61d0b53873 Mon Sep 17 00:00:00 2001 From: AlexanderS <122298605+skychel@users.noreply.github.com> Date: Mon, 9 Jan 2023 23:38:11 +0300 Subject: [PATCH] Update sockets.py If the library is used in Windows, then change time.time() to time.perf_counter() --- icmplib/sockets.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/icmplib/sockets.py b/icmplib/sockets.py index a2a428e..a8ecc7a 100644 --- a/icmplib/sockets.py +++ b/icmplib/sockets.py @@ -28,13 +28,24 @@ import socket, asyncio from struct import pack, unpack -from time import time +from time import time, perf_counter from .models import ICMPReply from .exceptions import * from .utils import PLATFORM_LINUX, PLATFORM_MACOS, PLATFORM_WINDOWS +''' +If OS Windows, then we use time.perf_counter(). Else - time.time() + +''' +if PLATFORM_WINDOWS: + get_time = perf_counter +else: + get_time = time + + + class ICMPSocket: ''' Base class for ICMP sockets. @@ -271,7 +282,7 @@ def send(self, request): self._set_ttl(request.ttl) self._set_traffic_class(request.traffic_class) - request._time = time() + request._time = get_time() self._sock.sendto(packet, sock_destination) # On Linux, the ICMP request identifier is replaced by the @@ -318,12 +329,12 @@ def receive(self, request=None, timeout=2): raise SocketUnavailableError self._sock.settimeout(timeout) - time_limit = time() + timeout + time_limit = get_time() + timeout try: while True: response = self._sock.recvfrom(1024) - current_time = time() + current_time = get_time() packet = response[0] source = response[1][0] @@ -737,7 +748,7 @@ async def receive(self, request=None, timeout=2): raise SocketUnavailableError loop = asyncio.get_running_loop() - time_limit = time() + timeout + time_limit = get_time() + timeout remaining_time = timeout try: @@ -746,7 +757,7 @@ async def receive(self, request=None, timeout=2): loop.sock_recv(self._icmp_sock._sock, 1024), remaining_time) - current_time = time() + current_time = get_time() if current_time > time_limit: raise asyncio.TimeoutError