Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specification of discovery interface. Closes #242 #277

Merged
merged 3 commits into from Jan 23, 2015

Conversation

Projects
None yet
4 participants
@lawrenceakka
Copy link
Contributor

commented Jan 17, 2015

This is based on ideas in #242. It allows an interface address to be specified when using discovery. Unlike #242 however it requires the user of the SoCo library to specify the interfaces to be used, rather than using the netifaces module to find all the interfaces on the local machine. This makes the code rather more simple, avoids the need for a dependency, and makes the user responsible for working out which interfaces to use based on knowledge of his or her system.

Thanks to @trollkarlen

@lawrenceakka lawrenceakka added this to the 0.11 milestone Jan 17, 2015

@coveralls

This comment has been minimized.

Copy link

commented Jan 17, 2015

Coverage Status

Coverage decreased (-0.1%) when pulling 70b9ec9 on lawrenceakka:change-discovery-interface into 99ba81c on SoCo:master.

@robwebset

This comment has been minimized.

Copy link
Contributor

commented Jan 18, 2015

Hi @lawrenceakka please can I suggest a couple of things:

  • Is it possible to maybe add a comment to the code with an example of how to get the information to call it - that way we won't lose the work that was done by @trollkarlen and it would make it easier for callers to work out the best way to use this - i.e. a comment covering what get_interfaces used to do with example code
  • Can the test added to #242 be reused?
@stefankoegl

This comment has been minimized.

Copy link
Member

commented Jan 18, 2015

+1 to @robwebset's comments, and - apart from that - +1 to the PR :)

@lawrenceakka

This comment has been minimized.

Copy link
Contributor Author

commented Jan 18, 2015

@robwebset,

Comments are no problem - I will add some. But what test are you talking about?

Lawrence

@robwebset

This comment has been minimized.

Copy link
Contributor

commented Jan 18, 2015

@lawrenceakka it looks like there were some test changes:

https://github.com/SoCo/SoCo/pull/242/files#diff-3

But I'm not too fussed about it :)

lawrenceakka added a commit that referenced this pull request Jan 23, 2015

Merge pull request #277 from lawrenceakka/change-discovery-interface
Allow specification of discovery interface. Closes #242

@lawrenceakka lawrenceakka merged commit 6b0979c into SoCo:master Jan 23, 2015

1 check was pending

continuous-integration/travis-ci The Travis CI build is in progress
Details
@lawrenceakka

This comment has been minimized.

Copy link
Contributor Author

commented Jan 23, 2015

All done. Thanks

@lawrenceakka lawrenceakka deleted the lawrenceakka:change-discovery-interface branch Jan 23, 2015

@robwebset

This comment has been minimized.

Copy link
Contributor

commented Jan 28, 2015

Hi All, I know it's closed, but I have just been making use of this - so thanks for the work you both put into this. As I need to use pretty basic Python (No binaries), I couldn't use "netifaces" so in case it's of any help to anyone else, I ended up doing the following:

    # Use the default method to search for Sonos Devices
    try:
        sonos_devices = soco.discover(timeout=5)
    except:
        log("SonosDiscovery: Exception when getting devices")
        log("SonosDiscovery: %s" % traceback.format_exc())
        sonos_devices = []

    # If there are multiple network devices, then this may not have picked up
    # the correct network device to broadcast on, so try another way
    if (sonos_devices is None) or (len(sonos_devices) < 1):
        try:
            # Try and find the address ourselves
            interfaceAddr = socket.gethostbyname(socket.gethostname())
            log("SonosDiscovery: Searching for devices using gethostname %s" % interfaceAddr)
            if interfaceAddr not in [None, '']:
                sonos_devices = soco.discover(timeout=5, interface_addr=interfaceAddr)
        except:
            log("SonosDiscovery: Exception when getting devices")
            log("SonosDiscovery: %s" % traceback.format_exc())
            sonos_devices = []

    # If still not found, try yet another method
    if (sonos_devices is None) or (len(sonos_devices) < 1):
        try:
            # Try and find the address ourselves
            interfaceAddr = socket.gethostbyname(socket.getfqdn())
            log("SonosDiscovery: Searching for devices using getfqdn %s" % interfaceAddr)
            if interfaceAddr not in [None, '']:
                sonos_devices = soco.discover(timeout=5, interface_addr=interfaceAddr)
        except:
            log("SonosDiscovery: Exception when getting devices")
            log("SonosDiscovery: %s" % traceback.format_exc())
            sonos_devices = []

    # If still not found, try the last option
    if (sonos_devices is None) or (len(sonos_devices) < 1):
        try:
            # Try and find the address ourselves by going to a web page
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect(("sonos.com",80))
            interfaceAddr = s.getsockname()[0]
            s.close()
            log("SonosDiscovery: Searching for devices using web search %s" % interfaceAddr)
            if interfaceAddr not in [None, '']:
                sonos_devices = soco.discover(timeout=5, interface_addr=interfaceAddr)
        except:
            log("SonosDiscovery: Exception when getting devices")
            log("SonosDiscovery: %s" % traceback.format_exc())
            sonos_devices = []

Wasn't 100% sure if this is the sort of thing we'd want to include in the default discovery code - so thought I'd post it.

Thanks
Rob

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.