Last active
February 10, 2019 21:53
-
-
Save SunnyBingoMe/54af7192e4a804d8d2d459725b1819fb to your computer and use it in GitHub Desktop.
get a non-127 ip address by python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
print([l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] | |
if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)), | |
s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, | |
socket.SOCK_DGRAM)]][0][1]]) if l][0][0]) | |
# limit: only one IP, not getting multi-NIC IPs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
print([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1]) | |
# limit: cannot get the IPs that are linked to hostname, often not working as non-127 IPs are often not linked. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment