I'm seeing 127.0.0.1 for $localip even when connecting to a remote broker. I've just looked around a bit, and I think this could be more reliable:
hostname = 'hippo'
port = 1883
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((hostname, port))
print(s.getsockname()[0])
s.close()
It opens a socket connection to the broker / port (we know it'll have to be online!) and returns the local address. Wrapped in a bit of error handling, it may be more exact.
I'm seeing
127.0.0.1
for$localip
even when connecting to a remote broker. I've just looked around a bit, and I think this could be more reliable:It opens a socket connection to the broker / port (we know it'll have to be online!) and returns the local address. Wrapped in a bit of error handling, it may be more exact.