mirror of
https://github.com/python-kasa/python-kasa.git
synced 2024-12-22 19:23:34 +00:00
Update get_ip function
This commit is contained in:
parent
6b411700b7
commit
d201e59096
@ -6,11 +6,9 @@ import asyncio
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
import sys
|
|
||||||
import uuid
|
import uuid
|
||||||
from collections.abc import Callable, Iterable
|
from collections.abc import Callable, Iterable
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from subprocess import check_output
|
|
||||||
|
|
||||||
import onvif # type: ignore[import-untyped]
|
import onvif # type: ignore[import-untyped]
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
@ -18,6 +16,7 @@ from onvif.managers import NotificationManager # type: ignore[import-untyped]
|
|||||||
|
|
||||||
from ...credentials import Credentials
|
from ...credentials import Credentials
|
||||||
from ...eventtype import EventType
|
from ...eventtype import EventType
|
||||||
|
from ...exceptions import KasaException
|
||||||
from ..smartcammodule import SmartCamModule
|
from ..smartcammodule import SmartCamModule
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -111,16 +110,20 @@ class Listen(SmartCamModule):
|
|||||||
await self.runner.shutdown()
|
await self.runner.shutdown()
|
||||||
|
|
||||||
async def _get_host_ip(self) -> str:
|
async def _get_host_ip(self) -> str:
|
||||||
def _get_host() -> str:
|
def get_ip() -> str:
|
||||||
if sys.platform == "win32":
|
# From https://stackoverflow.com/a/28950776
|
||||||
return socket.gethostbyname(socket.gethostname())
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
else:
|
s.settimeout(0)
|
||||||
res = check_output(["hostname", "-I"]) # noqa: S603, S607
|
try:
|
||||||
listen_ip, _, _ = res.decode().partition(" ")
|
# doesn't even have to be reachable
|
||||||
return listen_ip
|
s.connect(("10.254.254.254", 1))
|
||||||
|
ip = s.getsockname()[0]
|
||||||
|
finally:
|
||||||
|
s.close()
|
||||||
|
return ip
|
||||||
|
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
return await loop.run_in_executor(None, _get_host)
|
return await loop.run_in_executor(None, get_ip)
|
||||||
|
|
||||||
async def _start_server(self, listen_ip: str | None, listen_port: int) -> str:
|
async def _start_server(self, listen_ip: str | None, listen_port: int) -> str:
|
||||||
app = web.Application()
|
app = web.Application()
|
||||||
@ -132,7 +135,14 @@ class Listen(SmartCamModule):
|
|||||||
await self.runner.setup()
|
await self.runner.setup()
|
||||||
|
|
||||||
if not listen_ip:
|
if not listen_ip:
|
||||||
listen_ip = await self._get_host_ip()
|
try:
|
||||||
|
listen_ip = await self._get_host_ip()
|
||||||
|
except Exception as ex:
|
||||||
|
raise KasaException(
|
||||||
|
"Unable to determine listen ip starting "
|
||||||
|
f"listener for {self._device.host}",
|
||||||
|
ex,
|
||||||
|
) from ex
|
||||||
|
|
||||||
self.site = web.TCPSite(self.runner, listen_ip, listen_port)
|
self.site = web.TCPSite(self.runner, listen_ip, listen_port)
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user