jalmeroth / homie-python

A Python-implementation of the homie v2 convention.
https://github.com/marvinroger/homie
54 stars 15 forks source link

$localip #10

Closed jpmens closed 8 years ago

jpmens commented 8 years ago

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.

jalmeroth commented 8 years ago

@jpmens could you test and see if this works for you too?

jpmens commented 8 years ago

Looks good.