Created
March 30, 2013 17:21
-
-
Save fuzeman/5277535 to your computer and use it in GitHub Desktop.
Get local IPv4 address using sockets (cross-platform, std lib)
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
def get_default_v4_address(): | |
return get_default_address("8.8.8.8") | |
def get_default_address(host): | |
try: | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.connect((host, 80)) | |
address = s.getsockname()[0] | |
s.close() | |
except socket.gaierror, e: | |
return None | |
return address |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment