首页 > 代码库 > 通过UDP探测端到端MTU

通过UDP探测端到端MTU

P_MTU_D(Path MTU Discovery)

socket的UDP功能,去发现端到端MTU

#!/usr/bin/env python3
#-*- encoding:utf8 -*-
#Used UDP to learn the MTU of the end_to_end PATH_MTU

import IN,argparse,socket

if not hasattr(IN,IP_MTU):
    raise RuntimeError(cannot perform MTU discovery on this combination)

def send_big_datagram(host,port):
    sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    sock.setsockopt(socket.IPPROTO_IP,IN.IP_MTU_DISCOVER,IN.IP_PMTUDISC_DO)
    sock.connect((host,port))
    try:
        sock.send(b#*65535)
    except socket.error:
        print("also,the datagram did not make it")
        max_mtu = sock.getsockopt(socket.IPPROTO_IP,IN.IP_MTU)
        print(Actual MTU:{}.format(max_mtu))

if __name__ == __main__:
    parser = argparse.ArgumentParser(description=Send UDP Packet to Get PATH_MTU)
    parser.add_argument(host,help=the target host)
    parser.add_argument(-p,metavar=port,type=int,default=1060,help=UDP port((default 1060))
    args = parser.parse_args()
    send_big_datagram(args.host,args.p)

 

通过UDP探测端到端MTU