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?
相关问题
- 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
This will return you the IP address in the Ubuntu system as well as MacOS. The output will be the system IP address as like my IP: 192.168.1.10.
You can use the netifaces module. Just type:
in your command shell and it will install itself on default Python installation.
Then you can use it like this:
On my computer it printed:
Author of this module claims it should work on Windows, UNIX and Mac OS X.
I use this on my ubuntu machines:
This doesn't work.
This will work on most linux boxes:
Note: This is not using the standard library, but quite simple.
$ pip install pif
Variation on ninjagecko's answer. This should work on any LAN that allows UDP broadcast and doesn't require access to an address on the LAN or internet.