Answer the question
In order to leave comments, you need to log in
How does this definition of a private address work in Python?
Source: https://stackoverflow.com/a/28950776
Code
import socket
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
s.close()
return IP
Answer the question
In order to leave comments, you need to log in
As I understand it, it doesn't matter what the address is - all the magic is in getsockname, and the address there can be any sane outside the computer. Although 8.8.8.8 is just for the socket to knock outside the computer.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question