Last active
June 21, 2019 13:55
-
-
Save MrYsLab/ac533669c75977927586f5be9cc449fb to your computer and use it in GitHub Desktop.
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 | |
def get_ip(): | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
try: | |
# doesn't even have to be reachable | |
s.connect(('1.2.3.4', 1)) | |
my_ip_addr = s.getsockname()[0] | |
except: | |
my_ip_addr = '127.0.0.1' | |
finally: | |
s.close() | |
return my_ip_addr | |
print(get_ip()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment