import pyotherside import threading import socket import re # 'ST:urn:schemas-upnp-org:device:MediaRenderer:1', def slow_function(): msg = "\r\n".join([ 'M-SEARCH * HTTP/1.1', 'HOST:239.255.255.250:1900', 'MAN:"ssdp:discover"', 'ST:urn:schemas-upnp-org:device:MediaRenderer:1', 'MX:2', '', '', ]) bytesmsg = msg.encode() mr = "urn:schemas-upnp-org:device:MediaRenderer:1".encode() # mr = ".".encode() se = "(?Phttps?://[^\s]+)".encode() s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) s.settimeout(3) s.sendto(bytesmsg, ('239.255.255.250', 1900)) try: while True: data = s.recvfrom(65507) for line in data: if mr in line: url = re.search(se, line).group("url") pyotherside.send('progress', url) except socket.timeout: pyotherside.send('finished', "DONE") class Discoverer: def __init__(self): self.bgthread = threading.Thread() self.bgthread.start() def discover(self): if self.bgthread.is_alive(): return self.bgthread = threading.Thread(target=slow_function) self.bgthread.start() discoverer = Discoverer()