Finding local IP addresses using Python's stdlib

后端 未结 30 2576
北恋
北恋 2020-11-21 23:54

How can I find local IP addresses (i.e. 192.168.x.x or 10.0.x.x) in Python platform independently and using only the standard library?

30条回答
  •  有刺的猬
    2020-11-22 00:30

    One simple way to produce "clean" output via command line utils:

    import commands
    ips = commands.getoutput("/sbin/ifconfig | grep -i \"inet\" | grep -iv \"inet6\" | " +
                             "awk {'print $2'} | sed -ne 's/addr\:/ /p'")
    print ips
    

    It will show all IPv4 addresses on the system.

提交回复
热议问题