Search code examples
pythonsocketsmtu

How to find mtu value of network through code(in python)?


I have to write a code where I need to send data using udp protocol in python. I need to set the packet size to the MTU value of the network. Is there any way that I can decide the MTU value of the network writing some code in python?


Solution

  • This answer was taken from http://books.google.co.il/books?id=9HGUc8AO2xQC&pg=PA31&lpg=PA31&dq#v=onepage&q&f=false (page 31)

    s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    hostName = #ip here
    Port = 9999
    s.connect((hostName, Port))
    s.setsockopt(socket.IPPROTO_IP, IN.IP_MTU_DISCOVER, IN.IP_PMTUDISC_DO)
    try:
        s.send('#' * 1473)
    except socket.error:
        print 'The message did not make it'
        option = getattr(IN, 'IP_MTU', 14)
        print 'MTU:', s.getsockopt(socket.IPPROTO_IP, option)
    else:
        print 'The big message was sent! Your network supports really big packets!'