为什么 UPnPy 发现抛出 AttributeError?

发布于 2025-01-10 12:32:36 字数 1733 浏览 0 评论 0原文

我正在使用 UPnPy,当我尝试发现本地网络上的设备时,我立即注意到一个问题。这是我正在使用的基本代码:

import upnpy

upnp = upnpy.UPnP()

devices = upnp.discover()

这会引发以下异常:

Traceback (most recent call last):
  File "C:\Users\name\Projects\pythonProject\main.py", line 5, in <module>
    devices = upnp.discover()
  File "C:\Users\name\Projects\pythonProject\venv\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\name\Projects\pythonProject\venv\lib\site-packages\upnpy\ssdp\SSDPRequest.py", line 50, in m_search
    devices = self._send_request(self._get_raw_request())
  File "C:\Users\name\Projects\pythonProject\venv\lib\site-packages\upnpy\ssdp\SSDPRequest.py", line 100, in _send_request
    device = SSDPDevice(addr, response.decode())
  File "C:\Users\name\Projects\pythonProject\venv\lib\site-packages\upnpy\ssdp\SSDPDevice.py", line 87, in __init__
    self._get_services_request()
  File "C:\Users\name\Projects\pythonProject\venv\lib\site-packages\upnpy\ssdp\SSDPDevice.py", line 23, in wrapper
    return func(device, *args, **kwargs)
  File "C:\Users\name\Projects\pythonProject\venv\lib\site-packages\upnpy\ssdp\SSDPDevice.py", line 54, in wrapper
    return func(instance, *args, **kwargs)
  File "C:\Users\name\Projects\pythonProject\venv\lib\site-packages\upnpy\ssdp\SSDPDevice.py", line 171, in _get_services_request
    event_sub_url = service.getElementsByTagName('eventSubURL')[0].firstChild.nodeValue
AttributeError: 'NoneType' object has no attribute 'nodeValue'

我一直在研究其原因,但一无所获。我正在使用 UPnPy 版本 1.1.8。我使用 PyCharm 作为我的 IDE。我尝试过使用以前版本的 UPnPy,但似乎都不起作用。任何帮助将不胜感激。谢谢!

I am working with UPnPy, and I immediately notice an issue when attempting to discover devices on my local network. Here is the basic code I am using:

import upnpy

upnp = upnpy.UPnP()

devices = upnp.discover()

This throws the following exception:

Traceback (most recent call last):
  File "C:\Users\name\Projects\pythonProject\main.py", line 5, in <module>
    devices = upnp.discover()
  File "C:\Users\name\Projects\pythonProject\venv\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\name\Projects\pythonProject\venv\lib\site-packages\upnpy\ssdp\SSDPRequest.py", line 50, in m_search
    devices = self._send_request(self._get_raw_request())
  File "C:\Users\name\Projects\pythonProject\venv\lib\site-packages\upnpy\ssdp\SSDPRequest.py", line 100, in _send_request
    device = SSDPDevice(addr, response.decode())
  File "C:\Users\name\Projects\pythonProject\venv\lib\site-packages\upnpy\ssdp\SSDPDevice.py", line 87, in __init__
    self._get_services_request()
  File "C:\Users\name\Projects\pythonProject\venv\lib\site-packages\upnpy\ssdp\SSDPDevice.py", line 23, in wrapper
    return func(device, *args, **kwargs)
  File "C:\Users\name\Projects\pythonProject\venv\lib\site-packages\upnpy\ssdp\SSDPDevice.py", line 54, in wrapper
    return func(instance, *args, **kwargs)
  File "C:\Users\name\Projects\pythonProject\venv\lib\site-packages\upnpy\ssdp\SSDPDevice.py", line 171, in _get_services_request
    event_sub_url = service.getElementsByTagName('eventSubURL')[0].firstChild.nodeValue
AttributeError: 'NoneType' object has no attribute 'nodeValue'

I have been researching the cause of this but I have found nothing. I am using UPnPy version 1.1.8. I use PyCharm as my IDE. I've tried using previous versions of UPnPy but none seem to be working. Any help would be appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缪败 2025-01-17 12:32:36

您的家庭网络中很可能有一个不兼容的 UPnP 设备,该设备在其描述位置提供非标准/损坏的 XML,并且 upnpy 不够智能,无法处理解析错误,并且可能会忽略那个设备。

这种情况比您想象的更常见:许多智能电视(当然是 LG 的)都有一个宣传为 UPnP 的嵌入式设备,但它们的描述端点回答 JSON 而不是 XML!

一些建议:

  • 使用不同的库或应用程序(您可以尝试我自己的),至少可以识别罪魁祸首。检查打开详细程度并查找警告和解析错误日志
  • 使用诸如tcpdump之类的扫描仪来嗅探SSDPNOTICE广告的网络数据包(UDP端口1900),并手动打开每个浏览器中的 LOCATION URL,以查看它们是否是有效的 XML
  • 有选择地关闭/拔掉您认为可能支持 UPnP 的设备,例如智能电视、家庭影院、视频游戏控制台、路由器等,以查看哪一个正在提供伪造的 XML。
  • 编辑 upnpy 的本地副本来处理该错误,例如用 try/ except 块将函数/行括起来,并打印有关它之前尝试解析的内容的一些详细信息到错误。

Most likely you have a non-compliant UPnP Device in your home network that is serving a non-standard/broken XML at their description location, and upnpy was not smart enough to handle the parsing error and perhaps ignore that device.

That scenario is more common than you might think: many Smart TVs (LG ones for sure) have an embedded device that advertises as UPnP but their description endpoint answers JSON instead of XML!

Some suggestions:

  • Use a different library or app (you could try my own), at least to identify the culprit. Check turn on verbosity and look for warnings and parsing error logs
  • Use a scanner such as tcpdump to sniff network packets (UDP port 1900) for SSDP NOTICE advertises, and manually open each LOCATION URL in a browser to see if they're valid XML
  • Selectively turn off / unplug devices you think might be UPnP-enabled, such as Smart TVs, Home Theaters, Video Game consoles, Routers, etc, to see wich one was serving the bogus XML.
  • Edit your local copy of upnpy to handle that error, for example enclose the function/line with a try/except block and print some details about what is it trying to parse prior to the error.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文