Last active
July 30, 2020 08:23
-
-
Save fjolublar/31ef9ed0f62d09c09d106daa734e34bd to your computer and use it in GitHub Desktop.
Function to get own ip of the system in 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
#https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib #fatal_error | |
def get_ip(self): | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
try: | |
# doesn't even have to be reachable | |
s.connect(('10.255.255.255', 1)) | |
IP = s.getsockname()[0] | |
except Exception: | |
IP = '127.0.0.1' | |
finally: | |
s.close() | |
return IP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment