Skip to content

Instantly share code, notes, and snippets.

@fjolublar
Last active July 30, 2020 08:23
Show Gist options
  • Save fjolublar/31ef9ed0f62d09c09d106daa734e34bd to your computer and use it in GitHub Desktop.
Save fjolublar/31ef9ed0f62d09c09d106daa734e34bd to your computer and use it in GitHub Desktop.
Function to get own ip of the system in python.
#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