Skip to content

WSDiscovery

WS-Discovery implementation for python

Installation

pip install WSDiscovery

Example

WS-Discovery 서버와 클라이언트를 동시에 실행하는 예제는 다음과 같다:

from wsdiscovery.discovery import ThreadedWSDiscovery as WSDiscovery
from wsdiscovery.publishing import ThreadedWSPublishing as WSPublishing
from wsdiscovery import QName, Scope

# Define type, scope & address of service
ttype1 = QName("http://www.onvif.org/ver10/device/wsdl", "Device")
scope1 = Scope("onvif://www.onvif.org/Model")
xAddr1 = "localhost:8080/abc"

# Publish the service
wsp = WSPublishing()
wsp.start()
wsp.publishService(types=[ttype1], scopes=[scope1], xAddrs=[xAddr1])

# Discover it (along with any other service out there)
wsd = WSDiscovery()
wsd.start()
services = wsd.searchServices()
for service in services:
    print(service.getEPR() + ":" + service.getXAddrs()[0])
wsd.stop()

다음과 같이 출력 된다:

urn:uuid:03590000-e4f9-11b4-8389-5803fb986fcf:http://192.168.0.201/onvif/device_service
urn:uuid:4447033a-293c-4cff-a6aa-6599509515aa:localhost:8080/abc
urn:uuid:78c2c020-78c2-4c02-801d-78c2c0201d75:http://192.168.0.50/onvif/device_service

Example (Legacy)

from wsdiscovery.discovery import ThreadedWSDiscovery as WSDiscovery
from wsdiscovery import Scope
import re

def display(any_list):
    for item in any_list:
        print(item)

def fetch_devices():
    wsd = WSDiscovery()
    scope1 = Scope("onvif://www.onvif.org/Profile")
    wsd.start()
    services = wsd.searchServices(scopes=[scope1])
    ipaddresses = []
    for service in services:
    #filter those devices that dont have ONVIF service
        ipaddress = re.search('(\d+|\.)+', str(service.getXAddrs()[0])).group(0)
        ipaddresses.append(ipaddress)
        print(display(service.getScopes()))
        print('----------END')

    print(f'\nnumber of devices detected: {len(services)}')
    wsd.stop()
    return ipaddresses

if __name__ == "__main__":
    onvif_devices_IPs = fetch_devices()
    display(onvif_devices_IPs)

See also

Favorite site