Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding local IP addresses using Python's stdlib

How can I find local IP addresses (i.e. 192.168.x.x or 10.0.x.x) in Python platform independently and using only the standard library?

like image 696
UnkwnTech Avatar asked Oct 03 '08 12:10

UnkwnTech


People also ask

How do you find out what IP address a program is using?

Type "netstat -bn" and press "Enter" to get the list of IP addresses along with the processes that initiated the connections. The protocol, IP and status are displayed under each program.


1 Answers

I just found this but it seems a bit hackish, however they say tried it on *nix and I did on windows and it worked.

import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("8.8.8.8", 80)) print(s.getsockname()[0]) s.close() 

This assumes you have an internet access, and that there is no local proxy.

like image 67
UnkwnTech Avatar answered Oct 07 '22 13:10

UnkwnTech