5kyc0d3r / upnpy

Lightweight UPnP client library for Python.
https://upnpy.readthedocs.io
MIT License
60 stars 18 forks source link

example gives error #7

Open MaxCarritt opened 4 years ago

MaxCarritt commented 4 years ago

Hi, I installed upnpy, and ran the script from you example:

import upnpy

upnp = upnpy.UPnP()

# Discover UPnP devices on the network
# Returns a list of devices e.g.: [Device <Broadcom ADSL Router>]
devices = upnp.discover()

# Select the IGD
# alternatively you can select the device directly from the list
# device = devices[0]
device = upnp.get_igd()

and on line 7 I get the following error:

Traceback (most recent call last):
  File "C:\Users\VMUser\Documents\upnpy test.py", line 7, in <module>
    devices = upnp.discover()
  File "C:\Users\VMUser\AppData\Local\Programs\Python\Python38\lib\site-packages\upnpy\upnp\UPnP.py", line 33, in discover
    for device in self.ssdp.m_search(discover_delay=delay, st='upnp:rootdevice', **headers):
  File "C:\Users\VMUser\AppData\Local\Programs\Python\Python38\lib\site-packages\upnpy\ssdp\SSDPRequest.py", line 50, in m_search
    devices = self._send_request(self._get_raw_request())
  File "C:\Users\VMUser\AppData\Local\Programs\Python\Python38\lib\site-packages\upnpy\ssdp\SSDPRequest.py", line 100, in _send_request
    device = SSDPDevice(addr, response.decode())
  File "C:\Users\VMUser\AppData\Local\Programs\Python\Python38\lib\site-packages\upnpy\ssdp\SSDPDevice.py", line 87, in __init__
    self._get_services_request()
  File "C:\Users\VMUser\AppData\Local\Programs\Python\Python38\lib\site-packages\upnpy\ssdp\SSDPDevice.py", line 23, in wrapper
    return func(device, *args, **kwargs)
  File "C:\Users\VMUser\AppData\Local\Programs\Python\Python38\lib\site-packages\upnpy\ssdp\SSDPDevice.py", line 54, in wrapper
    return func(instance, *args, **kwargs)
  File "C:\Users\VMUser\AppData\Local\Programs\Python\Python38\lib\site-packages\upnpy\ssdp\SSDPDevice.py", line 173, in _get_services_request
    device_services[parsed_service_id] = self.Service(
  File "C:\Users\VMUser\AppData\Local\Programs\Python\Python38\lib\site-packages\upnpy\ssdp\SSDPDevice.py", line 258, in __init__
    self._get_state_variables_request()
  File "C:\Users\VMUser\AppData\Local\Programs\Python\Python38\lib\site-packages\upnpy\ssdp\SSDPDevice.py", line 39, in wrapper
    return func(service, *args, **kwargs)
  File "C:\Users\VMUser\AppData\Local\Programs\Python\Python38\lib\site-packages\upnpy\ssdp\SSDPDevice.py", line 363, in _get_state_variables_request
    root = minidom.parseString(service_description)
  File "C:\Users\VMUser\AppData\Local\Programs\Python\Python38\lib\xml\dom\minidom.py", line 1969, in parseString
    return expatbuilder.parseString(string)
  File "C:\Users\VMUser\AppData\Local\Programs\Python\Python38\lib\xml\dom\expatbuilder.py", line 925, in parseString
    return builder.parseString(string)
  File "C:\Users\VMUser\AppData\Local\Programs\Python\Python38\lib\xml\dom\expatbuilder.py", line 223, in parseString
    parser.Parse(string, True)
xml.parsers.expat.ExpatError: no element found: line 1, column 0
[Finished in 0.2s with exit code 1]
[shell_cmd: python -u "C:\Users\VMUser\Documents\upnpy test.py"]
nashant commented 4 years ago

I get a different error but another issue with upnp.discover():

>>> upnp.discover()
Traceback (most recent call last):
  File "/usr/lib/python3.6/http/client.py", line 897, in _get_hostport
    port = int(host[i+1:])
ValueError: invalid literal for int() with base 10: '49152wps_scpd.xml'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/dist-packages/upnpy/upnp/UPnP.py", line 33, in discover
    for device in self.ssdp.m_search(discover_delay=delay, st='upnp:rootdevice', **headers):
  File "/usr/local/lib/python3.6/dist-packages/upnpy/ssdp/SSDPRequest.py", line 50, in m_search
    devices = self._send_request(self._get_raw_request())
  File "/usr/local/lib/python3.6/dist-packages/upnpy/ssdp/SSDPRequest.py", line 100, in _send_request
    device = SSDPDevice(addr, response.decode())
  File "/usr/local/lib/python3.6/dist-packages/upnpy/ssdp/SSDPDevice.py", line 87, in __init__
    self._get_services_request()
  File "/usr/local/lib/python3.6/dist-packages/upnpy/ssdp/SSDPDevice.py", line 23, in wrapper
    return func(device, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/upnpy/ssdp/SSDPDevice.py", line 54, in wrapper
    return func(instance, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/upnpy/ssdp/SSDPDevice.py", line 179, in _get_services_request
    base_url=base_url
  File "/usr/local/lib/python3.6/dist-packages/upnpy/ssdp/SSDPDevice.py", line 257, in __init__
    self._get_description_request()
  File "/usr/local/lib/python3.6/dist-packages/upnpy/ssdp/SSDPDevice.py", line 289, in _get_description_request
    service_description = utils.make_http_request(self.base_url + self.scpd_url).read()
  File "/usr/local/lib/python3.6/dist-packages/upnpy/utils.py", line 81, in make_http_request
    return urllib.request.urlopen(request)
  File "/usr/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.6/urllib/request.py", line 526, in open
    response = self._open(req, data)
  File "/usr/lib/python3.6/urllib/request.py", line 544, in _open
    '_open', req)
  File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 1353, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/usr/lib/python3.6/urllib/request.py", line 1293, in do_open
    h = http_class(host, timeout=req.timeout, **http_conn_args)
  File "/usr/lib/python3.6/http/client.py", line 859, in __init__
    (self.host, self.port) = self._get_hostport(host, port)
  File "/usr/lib/python3.6/http/client.py", line 902, in _get_hostport
    raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
http.client.InvalidURL: nonnumeric port: '49152wps_scpd.xml'
Schyrsivochter commented 4 years ago

Confirming this happens on Linux, too:

Python 3.8.5 (default, Jul 28 2020, 12:59:40) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import upnpy
>>> upnp = upnpy.UPnP()
>>> upnp.discover()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/dist-packages/upnpy/upnp/UPnP.py", line 33, in discover
    for device in self.ssdp.m_search(discover_delay=delay, st='upnp:rootdevice', **headers):
  File "/usr/local/lib/python3.8/dist-packages/upnpy/ssdp/SSDPRequest.py", line 50, in m_search
    devices = self._send_request(self._get_raw_request())
  File "/usr/local/lib/python3.8/dist-packages/upnpy/ssdp/SSDPRequest.py", line 100, in _send_request
    device = SSDPDevice(addr, response.decode())
  File "/usr/local/lib/python3.8/dist-packages/upnpy/ssdp/SSDPDevice.py", line 87, in __init__
    self._get_services_request()
  File "/usr/local/lib/python3.8/dist-packages/upnpy/ssdp/SSDPDevice.py", line 23, in wrapper
    return func(device, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/upnpy/ssdp/SSDPDevice.py", line 54, in wrapper
    return func(instance, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/upnpy/ssdp/SSDPDevice.py", line 176, in _get_services_request
    device_services[parsed_service_id] = self.Service(
  File "/usr/local/lib/python3.8/dist-packages/upnpy/ssdp/SSDPDevice.py", line 270, in __init__
    self._get_state_variables_request()
  File "/usr/local/lib/python3.8/dist-packages/upnpy/ssdp/SSDPDevice.py", line 39, in wrapper
    return func(service, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/upnpy/ssdp/SSDPDevice.py", line 375, in _get_state_variables_request
    root = minidom.parseString(service_description)
  File "/usr/lib/python3.8/xml/dom/minidom.py", line 1969, in parseString
    return expatbuilder.parseString(string)
  File "/usr/lib/python3.8/xml/dom/expatbuilder.py", line 925, in parseString
    return builder.parseString(string)
  File "/usr/lib/python3.8/xml/dom/expatbuilder.py", line 223, in parseString
    parser.Parse(string, True)
xml.parsers.expat.ExpatError: no element found: line 1, column 0
heliomcp commented 3 years ago

Got another error "(invalid token)":

Python 3.7.2 (default, Jan 17 2019, 23:30:57)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import upnpy
>>> upnp = upnpy.UPnP()
>>> devices = upnp.discover()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/media/upnpy/upnpy/upnp/UPnP.py", line 33, in discover
    for device in self.ssdp.m_search(discover_delay=delay, st='upnp:rootdevice', **headers):
  File "/media/upnpy/upnpy/ssdp/SSDPRequest.py", line 50, in m_search
    devices = self._send_request(self._get_raw_request())
  File "/media/upnpy/upnpy/ssdp/SSDPRequest.py", line 100, in _send_request
    device = SSDPDevice(addr, response.decode())
  File "/media/upnpy/upnpy/ssdp/SSDPDevice.py", line 84, in __init__
    self._get_friendly_name_request()
  File "/media/upnpy/upnpy/ssdp/SSDPDevice.py", line 23, in wrapper
    return func(device, *args, **kwargs)
  File "/media/upnpy/upnpy/ssdp/SSDPDevice.py", line 127, in _get_friendly_name_request
    root = minidom.parseString(self.description)
  File "/usr/local/lib/python3.7/xml/dom/minidom.py", line 1968, in parseString
    return expatbuilder.parseString(string)
  File "/usr/local/lib/python3.7/xml/dom/expatbuilder.py", line 925, in parseString
    return builder.parseString(string)
  File "/usr/local/lib/python3.7/xml/dom/expatbuilder.py", line 223, in parseString
    parser.Parse(string, True)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 1, column 6
>>>
mkluge commented 3 years ago

Same error here on Linux Mint 20.1.

virtadpt commented 3 years ago

I was able to recreate all of these errors. Arch Linux (20210819). Python v3.9.6 UPnPy v1.1.8 installed with Pip.

nashant commented 2 years ago

Raising this from the dead, you can search for just IGDs with upnp.discover(ST='urn:schemas-upnp-org:device:InternetGatewayDevice:1')