-
-
Save sboisson/45b31604179e60d16092fb9c08962a29 to your computer and use it in GitHub Desktop.
Tiny python SSDP discovery library with no external dependencies - Python 2 & 3 compatible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 sys | |
import socket | |
try: | |
from http.client import HTTPResponse | |
except ImportError: | |
from httplib import HTTPResponse | |
class SSDPResponse(object): | |
def __init__(self, sock): | |
r = HTTPResponse(sock) | |
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] | |
def __repr__(self): | |
return "<SSDPResponse({location}, {st}, {usn})>".format(**self.__dict__) | |
def discover(service, timeout=2, retries=1, source_ip=None): | |
group = ("239.255.255.250", 1900) | |
message = "\r\n".join([ | |
'M-SEARCH * HTTP/1.1', | |
'HOST: {0}:{1}', | |
'MAN: "ssdp:discover"', | |
'ST: {st}', | |
'MX: 3','','' | |
]) | |
socket.setdefaulttimeout(timeout) | |
responses = {} | |
for _ in range(retries): | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | |
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
if source_ip: | |
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(source_ip)) | |
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2) | |
sock.sendto(message.format(*group, st=service).encode(), group) | |
print("SSDP Searching for %s from %s" % (service, source_ip or "any")) | |
while True: | |
try: | |
yield SSDPResponse(sock) | |
except socket.timeout: | |
break | |
SERVICE_TYPES = [ | |
"urn:schemas-upnp-org:device:InternetGatewayDevice:2", | |
"urn:schemas-upnp-org:service:WANIPConnection:2", | |
"urn:schemas-upnp-org:device:InternetGatewayDevice:1", | |
"urn:schemas-upnp-org:service:WANIPConnection:1", | |
"urn:schemas-upnp-org:service:WANPPPConnection:1", | |
"upnp:rootdevice", | |
"ssdp:all", | |
] | |
if __name__ == "__main__": | |
source = None | |
if len(sys.argv) > 1: | |
source = sys.argv[1] | |
for st in SERVICE_TYPES: | |
for rep in discover(st, timeout=2, source_ip=source): | |
print(" FOUND %s" % rep) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment