'Find IGD device and its control URL via UPnP multicast discovery'

def discovery(self, st='urn:schemas-upnp-org:device:InternetGatewayDevice:1'):

    logging.warning('Sending    multicast    traffic    looking    for    IGD    Devices    /    forwarding    TCP    port    ...')
    if (not self.isv6):
        up_disc = ('\r\n'.join(['M-SEARCH    *    HTTP/1.1', 'HOST:239.255.255.250:1900', 'ST:{}'.format(st), 'MX:2', 'MAN:"ssdp:discover"']) + ('\r\n' * 2))
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
        sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
        sock.bind((self.bindIP.format(), 19110))
        sock.sendto(up_disc, ('239.255.255.250', 1900))
    else:
        if self.bindIP.is_link_local():
            dst_ip = 'ff02::c'
        else:
            dst_ip = 'ff05::c'
            up_disc = ('\r\n'.join(['M-SEARCH    *    HTTP/1.1', 'HOST:[{dst}]:1900'.format(dst=dst_ip), 'ST:upnp:rootdevice', 'MX:2', 'MAN:"ssdp:discover"']) + ('\r\n' * 2))
        sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
        if self.debug:
            print 'trying    to    bind    to    address:', self.bindIP
        socketaddr = socket.getaddrinfo(self.bindIP.format(), 19110)[(-1):][0][(-1):][0]
        sock.bind(socketaddr)
        sock.sendto(up_disc, (dst_ip, 1900))
    if self.debug:
        print ('Discovery:    -----    tx    request    -----\n    ' + up_disc)
    sock.settimeout(self.timeout)
    try:
        (data, addr) = sock.recvfrom(1024)
    except socket.error:
        return
    sock.close()
    if self.debug:
        print ('Discovery:    -----    rx    reply    -----\n    ' + data)
    descURL = httpparse(StringIO(data)).getheader('location')
    if (not descURL):
        return
    try:
        descXMLs = urllib2.urlopen(descURL, None, self.timeout).read()
    except:
        return
    self.pr = urlparse(descURL)
    baseURL = ((self.pr.scheme + '://') + self.pr.netloc)
    dom = fromstring(descXMLs)
    if (self.igdsvc == 'WANIPC'):
        svctype = 'urn:schemas-upnp-org:service:WANIPConnection'
    else:
        svctype = 'urn:schemas-upnp-org:service:WANIPv6FirewallControl'
    for e in dom.findall('.//device:service', self.NS):
        stn = e.find('device:serviceType', self.NS)
        if (not (stn is None)):
            if (stn.text[0:(-2)] == svctype):
                cun = e.find('device:controlURL', self.NS).text
                self.ctrlURL = (baseURL + cun)
                break
    if self.debug:
        print 'control    URL    is    ', self.ctrlURL
