python-upnp-ssdp-example icon indicating copy to clipboard operation
python-upnp-ssdp-example copied to clipboard

Early launch of ssdp_server.py (from rc.local) will fail on Pi

Open philippeportesppo opened this issue 7 years ago • 0 comments

Hi, I thank you a lot for the code you wrote. I adapted it for my needs in my SmartThings porting to add UPNP detection of my Pi (see https://github.com/philippeportesppo/AirMentorPro2_SmartThings).

I found your original code in main.py had a problem to be launched when the system is early in booting, meaning the network interface is not yet available.

Since I modified your code too much to fork a version of it, I just snap the code below. The idea is to have a waiting stage till the network interface shows up.

Thanks again (ou merci si vous etes en France).

def get_network_interface_ip_address(interface='wlan0'):

"""

Get the first IP address of a network interface.

:param interface: The name of the interface.

:return: The IP address.

"""


while interface not in ni.interfaces():

    print('Could not find interface %s.' % (interface,))

    sleep(10)



while True:
    interfacestring = ni.ifaddresses(interface)

    if (2 not in interfacestring) or (len(interfacestring[2]) == 0):

        print('Could not find IP of interface %s. Sleeping.' % (interface,))

        sleep(10)

        continue
    else:
        break
return interfacestring[2][0]['addr']

philippeportesppo avatar Feb 13 '18 03:02 philippeportesppo