andreikop / python-ws-discovery

WS-Discovery implementation in Python3
GNU Lesser General Public License v3.0
85 stars 46 forks source link

OSError: [Errno 48] Address already in use #50

Closed nnnLik closed 1 year ago

nnnLik commented 1 year ago

I have a django project and one route where my function is called which searches for all ip cameras, but if I access this rout twice then the second time I get the following error

my function

def find(self):
    ips = list()
    for iface in netifaces.interfaces():
        if netifaces.AF_INET in netifaces.ifaddresses(iface):
            ips.append(netifaces.ifaddresses(iface)[netifaces.AF_INET][0]["addr"])
    scope = [".".join(ip.split(".")[:2]) for ip in ips]

    wsd = WSDiscovery()
    wsd.start()
    ret = wsd.searchServices()
    wsd.stop()
    onvif_services = [s for s in ret if str(s.getTypes()).find("onvif") >= 0]
    urls = [ip for s in onvif_services for ip in s.getXAddrs()]
    ips = [ip for url in urls for ip in re.findall(r"\d+\.\d+\.\d+\.\d+", url)]
    lst = [ip for ip in ips if any(ip.startswith(sp) for sp in scope)]
    return sorted(lst)

error

Traceback (most recent call last):
  File "/Users/macbook/Python-Proj/test/camera/lib/python3.9/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/Users/macbook/Python-Proj/test/camera/lib/python3.9/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/macbook/Python-Proj/test/camera/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/macbook/Python-Proj/test/camera/lib/python3.9/site-packages/rest_framework/viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/macbook/Python-Proj/test/camera/lib/python3.9/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/Users/macbook/Python-Proj/test/camera/lib/python3.9/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/macbook/Python-Proj/test/camera/lib/python3.9/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/Users/macbook/Python-Proj/test/camera/lib/python3.9/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/macbook/Python-Proj/test/camera/apps/Locations/views.py", line 54, in list
    all_cameras_ips = camera_finder.find()
  File "/Users/macbook/Python-Proj/test/camera/apps/Locations/service.py", line 19, in find
    wsd.start()
  File "/Users/macbook/Python-Proj/test/camera/lib/python3.9/site-packages/wsdiscovery/threaded.py", line 285, in start
    self._startThreads()
  File "/Users/macbook/Python-Proj/test/camera/lib/python3.9/site-packages/wsdiscovery/threaded.py", line 265, in _startThreads
    self._networkingThread.start()
  File "/Users/macbook/Python-Proj/test/camera/lib/python3.9/site-packages/wsdiscovery/threaded.py", line 239, in start
    self._multiInSocket = self._createMulticastInSocket()
  File "/Users/macbook/Python-Proj/test/camera/lib/python3.9/site-packages/wsdiscovery/threaded.py", line 109, in _createMulticastInSocket
    sock.bind(('', MULTICAST_PORT))
OSError: [Errno 48] Address already in use
nnnLik commented 1 year ago

No matter what web framework I use, this error is everywhere

nnnLik commented 1 year ago

If you implement it through a class, there will be no errors