Skip to content

Instantly share code, notes, and snippets.

@MattWoodhead
Created July 9, 2017 10:40
Show Gist options
  • Save MattWoodhead/541f23ecd9b464adf2b2e1598aa9261d to your computer and use it in GitHub Desktop.
Save MattWoodhead/541f23ecd9b464adf2b2e1598aa9261d to your computer and use it in GitHub Desktop.
OS agnostic method to find local IP address. Requires internet connection.
import socket
def ip_check():
""" uses the cockets module to find the local IP address """
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80)) # Ping Google DNS server
ip_address = s.getsockname()[0]
s.close()
return ip_address
print(ip_check())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment