Dmitry Telenkov 1 tháng trước cách đây
mục cha
commit
f93e008701

+ 22 - 3
python_net/aioarp/aioarp/_backends/_utils.py

@@ -19,6 +19,26 @@ class LocalNetwork:
     #     if interface is None:
     #         interface = self.
 
+    def get_mac(self, interface: typing.Optional[str] = None) -> str:
+        if interface is None:
+            interface = self.get_default_interface()
+        global OUR_MAC
+        if OUR_MAC:
+            return OUR_MAC
+
+        OUR_MAC = getmac.get_mac_address(interface)
+        return typing.cast(str, OUR_MAC)
+
+
+    def get_ip(self) -> str:
+        global OUR_IP
+        if OUR_IP:
+            return OUR_IP
+        with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
+            s.connect(("1.1.1.1", 80))
+            ip = typing.cast(str, s.getsockname()[0])
+            OUR_IP = ip
+            return OUR_IP
 
     def get_default_interface(self) -> str:
         global OUR_INTERFACE
@@ -26,8 +46,6 @@ class LocalNetwork:
             return OUR_INTERFACE
         output = subprocess.check_output(['ip', 'route', 'list']).decode().split()
 
-        return 'qwerty'
-
         for ind, word in enumerate(output):
             if word == 'dev':
                 OUR_INTERFACE = output[ind + 1]
@@ -38,5 +56,6 @@ class LocalNetwork:
 if __name__ == '__main__':
     foo = LocalNetwork()
     print(foo.get_default_interface())
-
+    print(foo.get_mac())
+    print(foo.get_ip())
 # print(getmac.get_mac_address('Ethernet 3'))

+ 1 - 1
python_net/aioarp/pyproject.toml

@@ -5,5 +5,5 @@ description = "Add your description here"
 readme = "README.md"
 requires-python = ">=3.13"
 dependencies = [
-    "getmac>=0.9.5",
+    "getmac==0.9.5",
 ]

+ 1 - 1
python_net/aioarp/uv.lock

@@ -11,7 +11,7 @@ dependencies = [
 ]
 
 [package.metadata]
-requires-dist = [{ name = "getmac", specifier = ">=0.9.5" }]
+requires-dist = [{ name = "getmac", specifier = "==0.9.5" }]
 
 [[package]]
 name = "getmac"