Advertisement
steve-shambles-2109

186-Get local IP address

Oct 23rd, 2019
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. '''
  2. Python code snippets vol 38:
  3. 186-Get local IP address
  4. stevepython.wordpress.com
  5.  
  6. requirements:None
  7.  
  8. source:
  9. https://gist.github.com/MattWoodhead/541f23ecd9b464adf2b2e1598aa9261d
  10. '''
  11. import socket
  12.  
  13. def ip_check():
  14.     """ uses the sockets module to find the local IP address """
  15.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  16.     s.connect(("8.8.8.8", 80))  # Ping Google DNS server
  17.     ip_address = s.getsockname()[0]
  18.     s.close()
  19.     return ip_address
  20.  
  21. print(ip_check())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement