1

我正在编写一个简单的程序来获取从我的机器到任意站点(在本例中为 www.google.com.

我的程序似乎卡在了 recvfrom() 调用上。我已将其设置为非阻塞套接字,因此我可以看到错误:

[WinError 10035] A non-blocking socket operation could not be completed immediately

当我将它作为阻塞套接字运行时,程序只会永远运行。

下面是我的源代码。知道为什么我会遇到这个问题吗?谢谢!

import socket
import select
import queue

def main(dest_name):
    print('launch')
    dest_addr = socket.gethostbyname(dest_name)
    # Define UDP and ICMP
    udp = socket.getprotobyname('udp')
    icmp = socket.getprotobyname('icmp')
    timer = 0
    port = 54321
    maxHops = 40
    while True:
        # Create sender and receiver. Sender uses UDP, receiver uses IDMP
        sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, udp)
        receiver = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
        receiver.setblocking(0)

        print('sockets assigned')
        # Assign TTL to sender, increment TTL
        sender.setsockopt(socket.SOL_IP, socket.IP_TTL, timer)
        print('ttl assigned')
        # Bind socket and send message from sender to receiver
        receiver.setblocking(0)
        receiver.bind(("", port))
        print('receiver bound')
        sender.sendto(bytes("", 'UTF-8'), (dest_name, port))
        print('sent')
        addr = None
        print('addr assigned')

        try:
            # Reads an array of 512-byte sized blocks from sender into addr
            (_,addr) = receiver.recvfrom(512)
            addr = addr[0]
            print("%f\n", addr)
       # Process socket errors
        except socket.error as exc:
            print('Error: please try again.\n')
            print('%s\n', exc)
            pass
        # Close both sockets
        finally:
           sender.close()
           receiver.close()
           print('closing')

        timer += 1

        if dest_addr == addr or timer == maxHops:
           break

if __name__ == "__main__":
    main('www.google.com')
4

1 回答 1

0

那个怎么样 :

#!/usr/bin/python

import socket

def main(dest_name):
    dest_addr = socket.gethostbyname(dest_name)
    port = 54321
    max_hops = 40
    icmp = socket.getprotobyname('icmp')
    udp = socket.getprotobyname('udp')
    ttl = 1
    while True:
        recv_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
        send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, udp)
        send_socket.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
        recv_socket.bind(("", port))
        send_socket.sendto("", (dest_name, port))
        curr_addr = None
        curr_name = None
        try:
            _, curr_addr = recv_socket.recvfrom(512)
            curr_addr = curr_addr[0]
            try:
                curr_name = socket.gethostbyaddr(curr_addr)[0]
            except socket.error:
                curr_name = curr_addr
        except socket.error:
            pass
        finally:
            send_socket.close()
            recv_socket.close()

        if curr_addr is not None:
            curr_host = "%s (%s)" % (curr_name, curr_addr)
        else:
            curr_host = "*"
        print "%d\t%s" % (ttl, curr_host)

        ttl += 1
        if curr_addr == dest_addr or ttl > max_hops:
            break

if __name__ == "__main__":
    main('google.com')
于 2015-12-03T20:43:35.147 回答