コード例 #1
0
        except ValueError:
            continue
    if (headers.get('x-user-agent', None) == 'redsonic'):
        location = headers.get('location', None)
        if location is not None and location not in CLIENTS:
            print("Found WeMo at {0}".format(location))
            CLIENTS[location] = headers
            #gevent.spawn(discovered.send, self, address=address,
            #        headers=headers)


server = DatagramServer(get_ip_address() + ':54321', _response_received)

request = '\r\n'.join(
    ("M-SEARCH * HTTP/1.1", "HOST:{}:{}", "ST:upnp:rootdevice", "MX:2",
     'MAN:"ssdp:discover"', "", "")).format(MCAST_IP, MCAST_PORT)

with gevent.Timeout(2, KeyboardInterrupt):
    while True:
        try:
            server.set_spawn(1)
            server.init_socket()
            server.socket.setsockopt(_socket.IPPROTO_IP,
                                     _socket.IP_MULTICAST_TTL, MCAST_TTL)
            server.start()
            server.sendto(request.encode(), (MCAST_IP, MCAST_PORT))
            gevent.sleep(2)
        except KeyboardInterrupt:
            print('Scan Complete')
            break