First check the UPnP path in Router.AddPortMapping() to avoid AttributeError

This commit is contained in:
Dmitri Bogomolov 2019-12-31 00:32:56 +02:00
parent 7b8bf082ff
commit 5cf541cfd3
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13

View File

@ -76,7 +76,7 @@ class UPnPError(Exception):
def __init__(self, message): def __init__(self, message):
super(UPnPError, self).__init__() super(UPnPError, self).__init__()
logger.error(message) # logger.error(message)
class Router: # pylint: disable=old-style-class class Router: # pylint: disable=old-style-class
@ -112,14 +112,15 @@ class Router: # pylint: disable=old-style-class
dom = parseString(directory) dom = parseString(directory)
self.name = dom.getElementsByTagName('friendlyName')[0].childNodes[0].data self.name = dom.getElementsByTagName('friendlyName')[0].childNodes[0].data
# find all 'serviceType' elements
service_types = dom.getElementsByTagName('serviceType')
for service in service_types: # find all 'serviceType' elements
if service.childNodes[0].data.find('WANIPConnection') > 0 or \ for service in dom.getElementsByTagName('serviceType'):
service.childNodes[0].data.find('WANPPPConnection') > 0: upnp_schema = service.childNodes[0].data.split(':')[-2]
self.path = service.parentNode.getElementsByTagName('controlURL')[0].childNodes[0].data if upnp_schema in ('WANIPConnection', 'WANPPPConnection'):
self.upnp_schema = service.childNodes[0].data.split(':')[-2] self.upnp_schema = upnp_schema
self.path = service.parentNode.getElementsByTagName(
'controlURL')[0].childNodes[0].data
break
def AddPortMapping( def AddPortMapping(
self, self,
@ -133,6 +134,9 @@ class Router: # pylint: disable=old-style-class
): # pylint: disable=too-many-arguments ): # pylint: disable=too-many-arguments
"""Add UPnP port mapping""" """Add UPnP port mapping"""
if not self.path:
raise UPnPError("No WAN connection")
resp = self.soapRequest(self.upnp_schema + ':1', 'AddPortMapping', [ resp = self.soapRequest(self.upnp_schema + ':1', 'AddPortMapping', [
('NewRemoteHost', ''), ('NewRemoteHost', ''),
('NewExternalPort', str(externalPort)), ('NewExternalPort', str(externalPort)),