Created
July 25, 2023 07:14
-
-
Save wention/123c1729ab31ef567a86b90466a8400d to your computer and use it in GitHub Desktop.
Get My IPAddress
This file contains hidden or 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_myip(): | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.settimeout(0) | |
myip = None | |
try: | |
# doesn't even have to be reachable | |
s.connect(('10.254.254.254', 1)) | |
myip = s.getsockname()[0] | |
except Exception: | |
pass | |
finally: | |
s.close() | |
return myip |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment