def init_ssdp_server(self): self._ssdp_server = SSDPServer(self._ssdp_address) for dev in self.upnp.devices(): n = Notification() n._type = "upnp:rootdevice" n._location = self._http_base_url + dev.location() n._udn = dev.udn() self._ssdp_server.add_notification(n) n = Notification() n._type = dev.udn() n._location = self._http_base_url + dev.location() n._udn = dev.udn() self._ssdp_server.add_notification(n) n = Notification() n._type = dev.type() n._location = self._http_base_url + dev.location() n._udn = dev.udn() self._ssdp_server.add_notification(n) for service in dev.services(): n = Notification() n._type = service.service_type() n._location = self._http_base_url + dev.location() n._udn = dev.udn() self._ssdp_server.add_notification(n) self._ssdp_server_thread = Thread(target=self._ssdp_server.serve_forever)
class UPnPServer: def __init__(self, upnp): self.upnp = upnp self._http_address = ("", 8080) self._ssdp_address = ("239.255.255.250", 1900) self._http_base_url = None self._iface = "eth0" self._http_server = None self._ssdp_server = None self._http_server_thread = None self._ssdp_server_thread = None def set_http_address(self, host=None, port=None, iface = None, base_url = None): if iface is not None: self._iface = iface host = get_ip_address(self._iface) if host is not None: self._http_address = (host, self._http_address[1]) if port is not None: self._http_address = (self._http_address[0], port) if base_url is not None: self._http_base_url = base_url else: self._http_base_url = "http://%s:%u" % self._http_address def set_ssdp_address(self, host=None, port=None): if host is not None: self._ssdp_address[0] = host if port is not None: self._ssdp_address[1] = port def http_address(self): return self._http_address def ssdp_address(self): return self._ssdp_address def base_url(self): return self._http_base_url def init_servers(self): self.init_http_server() self.init_ssdp_server() def init_http_server(self): self._http_server = HTTPResourceServer(self._http_address) self._http_server.add_resources(self.upnp.resources()) self._http_server_thread = Thread(target=self._http_server.serve_forever) def init_ssdp_server(self): self._ssdp_server = SSDPServer(self._ssdp_address) for dev in self.upnp.devices(): n = Notification() n._type = "upnp:rootdevice" n._location = self._http_base_url + dev.location() n._udn = dev.udn() self._ssdp_server.add_notification(n) n = Notification() n._type = dev.udn() n._location = self._http_base_url + dev.location() n._udn = dev.udn() self._ssdp_server.add_notification(n) n = Notification() n._type = dev.type() n._location = self._http_base_url + dev.location() n._udn = dev.udn() self._ssdp_server.add_notification(n) for service in dev.services(): n = Notification() n._type = service.service_type() n._location = self._http_base_url + dev.location() n._udn = dev.udn() self._ssdp_server.add_notification(n) self._ssdp_server_thread = Thread(target=self._ssdp_server.serve_forever) def start(self, init_servers = True): if init_servers: self.init_servers() self._http_server_thread.start() self._ssdp_server_thread.start() def wait(self): self._ssdp_server_thread.join() self._http_server_thread.join() def shutdown(self): self._http_server.shutdown() self._ssdp_server.shutdown()