python-library-lan-router 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
lan_router/__init__.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from .device import Device
|
|
2
|
+
from .base import BaseRouter
|
|
3
|
+
from .tplink import TPLinkRouter
|
|
4
|
+
from typing import Literal
|
|
5
|
+
|
|
6
|
+
def create_router(vendor: Literal["tplink"],**kwargs) -> BaseRouter:
|
|
7
|
+
if vendor == "tplink":
|
|
8
|
+
return TPLinkRouter(**kwargs)
|
|
9
|
+
else:
|
|
10
|
+
raise ValueError(f"不支持的厂商: {vendor}")
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"Device",
|
|
14
|
+
"BaseRouter",
|
|
15
|
+
"TPLinkRouter",
|
|
16
|
+
]
|
lan_router/base.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from pydantic import BaseModel
|
|
2
|
+
from abc import ABC, abstractmethod
|
|
3
|
+
from .device import Device
|
|
4
|
+
|
|
5
|
+
class BaseRouter(BaseModel, ABC):
|
|
6
|
+
hostname: str
|
|
7
|
+
"""路由器主机名"""
|
|
8
|
+
username: str
|
|
9
|
+
"""路由器用户名"""
|
|
10
|
+
password: str
|
|
11
|
+
"""路由器密码"""
|
|
12
|
+
|
|
13
|
+
@abstractmethod
|
|
14
|
+
def scan(self) -> list[Device]:
|
|
15
|
+
"""扫描设备"""
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
def login(self):
|
|
19
|
+
"""登录"""
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
def logout(self):
|
|
23
|
+
"""登出"""
|
|
24
|
+
pass
|
lan_router/device.py
ADDED
lan_router/tplink.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from .base import BaseRouter
|
|
2
|
+
from .device import Device
|
|
3
|
+
from tplinkrouterc6u import TPLinkXDRClient
|
|
4
|
+
|
|
5
|
+
class TPLinkRouter(BaseRouter):
|
|
6
|
+
_router: TPLinkXDRClient = None
|
|
7
|
+
|
|
8
|
+
def model_post_init(self,ctx):
|
|
9
|
+
self._router = TPLinkXDRClient(self.hostname, self.username, self.password)
|
|
10
|
+
|
|
11
|
+
def login(self):
|
|
12
|
+
self._router.authorize()
|
|
13
|
+
|
|
14
|
+
def logout(self):
|
|
15
|
+
self._router.logout()
|
|
16
|
+
|
|
17
|
+
def scan(self) -> list[Device]:
|
|
18
|
+
devices = []
|
|
19
|
+
status = self._router.get_status()
|
|
20
|
+
|
|
21
|
+
for data in status.devices:
|
|
22
|
+
type = getattr(data.type, "value", str(data.type))
|
|
23
|
+
name = data.hostname or ""
|
|
24
|
+
ip = self._show(data.ipaddr)
|
|
25
|
+
mac = self._show(data.macaddr)
|
|
26
|
+
device = Device(name=name, ip=ip, mac=mac, type=type)
|
|
27
|
+
devices.append(device)
|
|
28
|
+
|
|
29
|
+
return devices
|
|
30
|
+
|
|
31
|
+
def _show(self, v: str | None) -> str:
|
|
32
|
+
"""显示空值"""
|
|
33
|
+
return "-" if v is None or v == "" else v
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
lan_router/__init__.py,sha256=_I2EdWqJCiC1uH6mE5CLaOrjjexKG1b6yE5h7c6WEBk,400
|
|
2
|
+
lan_router/base.py,sha256=tbX-u7ELBcbPDOTvNsKGI1am-9_zZOOVTW2iB88zQDc,499
|
|
3
|
+
lan_router/device.py,sha256=_GRgYD8mQQEwYW9IgHM3EjhbNMUJ2yMPvyVu27qulOE,216
|
|
4
|
+
lan_router/tplink.py,sha256=3dT6LN5wgWLNo9ynCWOgf9oqm9IL-SBRHgFEK7qhJkM,999
|
|
5
|
+
python_library_lan_router-0.1.0.dist-info/METADATA,sha256=mfv08-uOqDloEJJivkXKEDQUA9PLJbe_cF91OP-vL0w,162
|
|
6
|
+
python_library_lan_router-0.1.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
7
|
+
python_library_lan_router-0.1.0.dist-info/RECORD,,
|