我正在编写一个简单的程序来获取从我的机器到任意站点(在本例中为 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')