class SSDP_Client(DatagramProtocol): def __init__(self, ssdp, interface, bind=None, notifyInterval=1800): self.ssdp = ssdp self.interface = interface self.notifySequenceInterval = notifyInterval self.notifySequenceLoop = task.LoopingCall(self._notifySequenceCall) self.running = False self.bind = bind def listen(self): if self.running: raise Exception() Logr.debug("listen()") self.listen_port = reactor.listenUDP(0, self, self.interface) self.running = True Logr.debug("listening on %s", self.listen_port.socket.getsockname()) reactor.callLater(0, self._notifySequenceCall, True) self.notifySequenceLoop.start(self.notifySequenceInterval) def stop(self): Logr.debug("stop()") if not self.running: return self.notifySequenceLoop.stop() self.listen_port.stopListening() def respond(self, headers, (address, port)): Logr.debug("respond() %s %d", address, port) msg = 'HTTP/1.1 200 OK\r\n' msg += headers_join(headers) msg += '\r\n\r\n' try: self.transport.write(msg, (address, port)) except socket.error, e: Logr.warning("socket.error: %s", e)
def respond(self, headers, (address, port)): Logr.debug("respond() %s %d", address, port) msg = 'HTTP/1.1 200 OK\r\n' msg += headers_join(headers) msg += '\r\n\r\n' try: self.transport.write(msg, (address, port)) except socket.error, e: Logr.warning("socket.error: %s", e) def send(self, method, headers, (address, port)): Logr.debug("send() %s:%s", address, port) msg = '%s * HTTP/1.1\r\n' % method msg += headers_join(headers) msg += '\r\n\r\n' try: self.transport.write(msg, (address, port)) except socket.error, e: Logr.warning("socket.error: %s", e) def send_NOTIFY(self, nt, uuid=None, nts='ssdp:alive'): if self.ssdp.device.bootID is None: self.ssdp.device.bootID = int(time.time()) if not self.bind: location = self.ssdp.device.getLocation(get_default_v4_address()) else: location = self.ssdp.device.getLocation(self.bind)
def respond(self, headers, (address, port)): Logr.debug("respond() %s %d", address, port) msg = 'HTTP/1.1 200 OK\r\n' msg += headers_join(headers) msg += '\r\n\r\n' try: self.transport.write(msg, (address, port)) except socket.error, e: Logr.warning("socket.error: %s", e) def send(self, method, headers, (address, port)): Logr.debug("send() %s:%s", address, port) msg = '%s * HTTP/1.1\r\n' % method msg += headers_join(headers) msg += '\r\n\r\n' try: self.transport.write(msg, (address, port)) except socket.error, e: Logr.warning("socket.error: %s", e) def send_NOTIFY(self, nt, uuid=None, nts='ssdp:alive'): if self.ssdp.device.bootID is None: self.ssdp.device.bootID = int(time.time()) location = self.ssdp.device.getLocation(get_default_v4_address()) if uuid is None: uuid = self.ssdp.device.uuid