[Python] How can Windows 10/11 influence raw sockets?

Discussão em 'Python' iniciado por Stack, Maio 2, 2025.

  1. Stack

    Stack Membro Participativo

    I'm currently working on a function that is ultimately an ICMP ping, no matter what though the connection times out.

    def icmp_ping(data, host, timeout: float):
    """"Basic ICMP response ping"""
    icmp_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
    icmp_socket.settimeout(timeout)
    pid = os.getpid() & 0xFFFF
    packet = build_icmp_packet(pid, 1, data)
    rt = time.time()

    try:
    host_byname = socket.gethostbyname(host)
    icmp_socket.sendto(packet, (host_byname, 0))
    recv_packet, addr = icmp_socket.recvfrom(1024)
    end_time = (time.time() - rt) * 1000
    print(f"Ping to {host}. RTT: {end_time:.2f}ms\nRecieved: {repr(recv_packet)} from {addr}")
    except socket.timeout as err:
    print(f"Connection to {host} || {host_byname} timed out after {timeout}s. Error: {err}")
    except PermissionError as err:
    print(f"You aren't allowed, {err}")
    except socket.gaierror as err:
    print(f"Could not resolve the domain {host} || {host_byname}. Error: {err}")
    finally:
    icmp_socket.close()


    I have tried a vast array of possible hosts. Namely:

    8.8.8.8
    example.com
    127.0.0.1


    All of which timeout, 127.0.0.1 was surprisingly a failure because I had hosted a dedicated server on my own machine at that IP.

    Example output:

    host 8.8.8.8
    Output: Connection to 8.8.8.8 || 8.8.8.8 timed out after 30s. Error: timed out

    host 127.0.0.1
    Output: Connection to 127.0.0.1 || 127.0.0.1 timed out after 30s. Error: timed out

    Continue reading...

Compartilhe esta Página