-
-
Save derflocki/10454054 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 socket | |
import httplib | |
import StringIO | |
import struct | |
class SSDPResponse(object): | |
class _FakeSocket(StringIO.StringIO): | |
def makefile(self, *args, **kw): | |
return self | |
def __init__(self, response): | |
self.orig_response = response | |
if(response[0:13] == 'NOTIFY * HTTP'): | |
response = response.replace("NOTIFY * HTTP/1.1","HTTP/1.1 200 OK") | |
self.parsed_response = 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") | |
if(self.cache): | |
self.cache = self.cache.split("=")[1] | |
self.nts = r.getheader("nts") | |
if self.nts: | |
self.st = r.getheader("nt") | |
def __repr__(self): | |
return "<SSDPResponse({location}, {st}, {usn}, {nts})>".format(**self.__dict__) | |
def discover(service, timeout=2, retries=1): | |
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','','']) | |
if(timeout): | |
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) | |
mreq = struct.pack('4sl', socket.inet_aton('239.255.255.250'), socket.INADDR_ANY) # pack MCAST_GRP correctly | |
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) # Request MCAST_GRP | |
#sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2) | |
sock.bind(('',1900)) # Bind to all intfs | |
sock.sendto(message.format(*group, st=service), group) | |
while True: | |
try: | |
data = sock.recv(1024) | |
except socket.timeout: | |
break | |
if(data): | |
try: | |
response = SSDPResponse(data) | |
if response.location and (service == "ssdp:all" or service == response.st): | |
yield response | |
except: | |
pass | |
# Example: | |
# import ssdp | |
# ssdp.discover("roku:ecp") | |
if __name__ == '__main__': | |
for resp in discover("urn:schemas-upnp-org:device:MediaRenderer:1", 0): | |
print resp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment