i using netifaces in python to get local ip address, but how to pick currently used (by system) network interface? Help me please. Thank you!
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
It's still not entirely clear what you're asking, because a system does not necessarily have a singled "used" interface. A system may have multiple interfaces with addresses, and may be using all of them to contact different systems.
A system will usually (but not always!) have a "default route" out one interface, which is typically used to contact hosts to which the system is not directly connected. If this is what you want, you can use the Python netifaces module, like this:
This will get you the name of the device used by the default IPv4 route. You can get the MAC address of that interface like this:
You can of course get the same information by parsing the output of the
ip
command, but using thenetifaces
module is much cleaner.