Skip to content

Instantly share code, notes, and snippets.

@wention
Created July 25, 2023 07:14
Show Gist options
  • Save wention/123c1729ab31ef567a86b90466a8400d to your computer and use it in GitHub Desktop.
Save wention/123c1729ab31ef567a86b90466a8400d to your computer and use it in GitHub Desktop.
Get My IPAddress
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