Page 1 of 1

External ip address

Posted: Tue Jan 23, 2018 7:00 am
by PlanB
I like this script because it will deliver the pi external ip address without asking a website like info.io to get it
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try: s.connect(('10.255.255.255', 1)); IP = s.getsockname()[0] # doesn't even have to be reachable
except: IP = 'unknown'
finally: s.close()
print IP
Unfortunately it only works for 3G dongles not wifi dongles working through a router. Networking confuses me. Why is it usual to ask someone else for your own external ip address? Why can't the network fess up itself?

Re: External ip address

Posted: Tue Jan 23, 2018 11:04 am
by pcmanbob
Most 3G connections do not actually have a true wan address they have an address on your service providers network, so you are sitting behind there router in much the same way as a wifi connection to a router when using a broadband connection.

most 3g providers use CgNAT https://en.wikipedia.org/wiki/Carrier-grade_NAT

Re: External ip address

Posted: Wed Jan 24, 2018 1:17 pm
by PlanB
Can someone explain the difference between

print((([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")] or [[(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) + ["no IP found"])[0])

and

print((([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1], [[(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) + ["no IP found"])[0])

the latter's claim to fame is it works on LANs without an internet connection, but on testing they both do.