Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find my Ip address in python [duplicate]

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 743
UnkwnTech Avatar asked Nov 26 '22 12:11

UnkwnTech


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 83
UnkwnTech Avatar answered Nov 28 '22 01:11

UnkwnTech