直接上代码:#!/usr/bin/python3# -*- coding: utf-8 -*-# 技术支持:https://www.jianshu.com/u/69f40328d4f0 # 技术支持 https://china-testing.github.io/# https://github.com/china-testing/python-api-tesing/blob/master/practices/traceroute.py# 项目实战讨论QQ群630011153 144081101# CreateDate: 2018-11-26import socketimport ioimport structimport sysclass flushfile(io.FileIO):
def __init__(self, f):
self.f = f def write(self, x):
self.f.write(x)
self.f.flush()
sys.stdout = flushfile(sys.stdout)def main(dest_name):
dest_addr = socket.gethostbyname(dest_name)
port = 55285
max_hops = 30
ttl = 1
while True:
rec_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
send_socket.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)