Я написал простой скрипт traceroute на Python 3, используя Sockets, но я никогда не получал ответа от сервера Google.Что не так с этим решением?
import socket
BIND_ADDRESS = "0.0.0.0" # all interfaces
PORT = 33434 # default port
ICMP = socket.getprotobyname('icmp')
UDP = socket.getprotobyname('udp')
def main(dest_url):
dest_ip = socket.gethostbyname(dest_url)
with socket.socket(socket.AF_INET, socket.SOCK_RAW, ICMP) as receiver:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM, UDP) as sender:
receiver.bind((BIND_ADDRESS, PORT))
ttl = 0
while True:
ttl += 1
sender.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
sender.sendto(b'', (dest_ip, PORT))
# sender.sendto(b'', (dest_url, PORT)) # same problem
print('Check 1')
current_server = receiver.recv(512)
print('Check 2') # Never prints: recv never ends
yield current_server
if current_server == dest_url:
break
print(*main('google.com'))
# print(*main('localhost')) # even this doesn't work