2 This module parse an UPnP device's XML definition in an Object.
4 @author: Raphael Slinckx
5 @copyright: Copyright 2005
7 @contact: U{raphael@slinckx.net<mailto:raphael@slinckx.net>}
13 from xml.dom import minidom
16 # Allowed UPnP services to use when mapping ports/external addresses
17 WANSERVICES = ['urn:schemas-upnp-org:service:WANIPConnection:1',
18 'urn:schemas-upnp-org:service:WANPPPConnection:1']
22 This objects parses the XML definition, and stores the useful
23 results in attributes.
25 The device infos dictionnary may contain the following keys:
26 - friendlyname: A friendly name to call the device.
27 - manufacturer: A manufacturer name for the device.
29 Here are the different attributes:
30 - deviceinfos: A dictionnary of device infos as defined above.
31 - controlurl: The control url, this is the url to use when sending SOAP
32 requests to the device, relative to the base url.
33 - wanservice: The WAN service to be used, one of the L{WANSERVICES}
34 - urlbase: The base url to use when talking in SOAP to the device.
36 The full url to use is obtained by urljoin(urlbase, controlurl)
39 def __init__(self, xml):
41 Parse the given XML string for UPnP infos. This creates the attributes
42 when they are found, or None if no value was found.
44 @param xml: a xml string to parse
46 logging.debug("Got UPNP Xml description:\n%s", xml)
47 doc = minidom.parseString(xml)
49 # Fetch various device info
53 'friendlyname':'friendlyName',
54 'manufacturer' : 'manufacturer'
56 device = doc.getElementsByTagName('device')[0]
57 for name, tag in attributes.iteritems():
59 self.deviceinfos[name] = device.getElementsByTagName(
60 tag)[0].firstChild.datas.encode('utf-8')
66 # Fetch device control url
67 self.controlurl = None
68 self.wanservice = None
70 for service in doc.getElementsByTagName('service'):
72 stype = service.getElementsByTagName(
73 'serviceType')[0].firstChild.data.encode('utf-8')
74 if stype in WANSERVICES:
75 self.controlurl = service.getElementsByTagName(
76 'controlURL')[0].firstChild.data.encode('utf-8')
77 self.wanservice = stype
85 self.urlbase = doc.getElementsByTagName(
86 'URLBase')[0].firstChild.data.encode('utf-8')