代码实现 使用ws-discovery库来发现网络上的ONVIF设备
时间: 2023-08-12 14:09:03 浏览: 96
好的,以下是使用ws-discovery库发现网络上的ONVIF设备的示例代码:
```python
from wsdiscovery.discovery import ThreadedWSDiscovery as WSDiscovery
# 发现设备
def discover_onvif_devices():
wsd = WSDiscovery(timeout=5)
wsd.start()
ret = wsd.searchServices(types=['dn:NetworkVideoTransmitter'], timeout=5)
wsd.stop()
return ret
# 打印设备信息
def print_device_info(devices):
for device in devices:
print("Service Address: " + device.getXAddrs()[0])
print("Hardware ID: " + device.getHardwareId())
print("Scopes: " + str(device.getScopes()))
print("Types: " + str(device.getTypes()))
print("")
# 示例代码运行
if __name__ == "__main__":
devices = discover_onvif_devices()
print_device_info(devices)
```
上述代码中,我们使用了 `wsdiscovery` 库来发现网络上的 ONVIF 设备。在 `discover_onvif_devices` 函数中,我们首先创建了一个 `ThreadedWSDiscovery` 对象,并指定了超时时间为 5 秒。然后,我们调用 `searchServices` 方法来搜索类型为 `dn:NetworkVideoTransmitter` 的设备,超时时间同样为 5 秒。最后,我们停止搜索并返回搜索结果。
在 `print_device_info` 函数中,我们遍历搜索结果并打印每个设备的信息,包括服务地址、硬件 ID、范围和类型。
在示例代码运行时,我们调用 `discover_onvif_devices` 函数发现设备,并调用 `print_device_info` 函数打印设备信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)