Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update sockets.py #62

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions icmplib/sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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



Comment on lines +38 to +48

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @TerjeMjelde in #61 (comment)

there's no need to differentiate between Linux and Windows here.

class ICMPSocket:
'''
Base class for ICMP sockets.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down