#!python
#vim: ai et ts=4 sts=4
#-*- coding: UTF-8 -*-
'''Simple SSDP query.
Stolen from https://gist.github.com/dankrause/6000248
'''
#   Copyright 2014 Dan Krause
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import socket
import httplib
import StringIO

class SSDPResponse(object):
    class _FakeSocket(StringIO.StringIO):
        def makefile(self, *args, **kw):
            return self
    def __init__(self, response):
        r = httplib.HTTPResponse(self._FakeSocket(response))
        r.begin()
        self.location = r.getheader("location")
        self.usn = r.getheader("usn")
        self.st = r.getheader("st")
        self.cache = r.getheader("cache-control").split("=", 1).pop()
        r.close()
    def __repr__(self):
        return "<SSDPResponse({location}, {st}, {usn})>".format(**self.__dict__)

def _try_once(message, address):
    sock = None
    responses = {}
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
        sock.sendto(message, address)
        while True:
            try:
                response = SSDPResponse(sock.recv(1024))
                responses[response.location] = response
            except socket.timeout:
                return responses
    finally:
        if sock is not None:
            sock.close()

def discover(service, timeout=5, retries=1, mx=3):
    group = ("239.255.255.250", 1900)
    message = "\r\n".join([
        'M-SEARCH * HTTP/1.1',
        'HOST: {0}:{1}',
        'MAN: "ssdp:discover"',
        'ST: {st}','MX: {mx}',
        'USER-AGENT: Nekst/0.0 UPnP/1.1 box/0.0.0',
        '',''])
    socket.setdefaulttimeout(timeout)
    responses = {}
    for _ in range(retries):
        responses.update(_try_once(message.format(*group, st=service, mx=mx), group))
    return responses.values()

if __name__ == '__main__':
    import sys
    for r in discover((len(sys.argv) > 1) and sys.argv[1] or 'ssdp:all', mx=2):
        for a in dir(r):
            if not a.startswith('_'):
                v = getattr(r,a)
                print('%s=%s' % (a, `v`))

#vim: ai et ts=4 sts=4
# EOF #
