1111import platform
1212import psutil
1313import ipaddress
14+ import os
1415from typing import Callable
1516
1617from . import Device
@@ -39,9 +40,6 @@ def __init__(self, ip=None, discovery_port=51337):
3940 self .target_interfaces = []
4041 self .discovery_port = discovery_port
4142 self .get_interface_from_ip (ip )
42- logger .info (f"Found { len (self .target_interfaces )} network interfaces:" )
43- for iface in self .target_interfaces :
44- logger .info (f" - { iface .name } : { iface .addr_str } " )
4543
4644 def get_interface_from_ip (self , ip ):
4745 if ip is None :
@@ -55,24 +53,32 @@ def get_interface_from_ip(self, ip):
5553 ip_list = ip
5654 else :
5755 raise TypeError
56+ logger .info (f"Found { len (psutil .net_if_stats ().items ())} total network interfaces, listing IPv4 interfaces:" )
5857
5958 for interface in psutil .net_if_stats ().items ():
6059 for interface_info in psutil .net_if_addrs ()[interface [0 ]]:
6160 # Only look for IPV4 binds
62- if socket .AF_INET == interface_info .family and (
63- interface_info .address in ip_list or ip_list [0 ] == "any"
64- ):
65- self .target_interfaces .append (
66- PyStageLinQ_interface_info (
67- interface [0 ],
68- len (self .target_interfaces ),
69- int (ipaddress .IPv4Address (interface_info .address )),
70- interface_info .address ,
71- int (ipaddress .IPv4Address (interface_info .netmask )),
72- interface [1 ],
73- 0 ,
61+ if socket .AF_INET == interface_info .family :
62+ logger .info (f" - { interface [0 ]} : { interface_info .address } " )
63+ if interface_info .address in ip_list or ip_list [0 ] == "any" :
64+ self .target_interfaces .append (
65+ PyStageLinQ_interface_info (
66+ interface [0 ],
67+ len (self .target_interfaces ),
68+ int (ipaddress .IPv4Address (interface_info .address )),
69+ interface_info .address ,
70+ int (ipaddress .IPv4Address (interface_info .netmask )),
71+ interface [1 ],
72+ 0 ,
73+ )
7474 )
75- )
75+
76+ logger .info (f"{ len (self .target_interfaces )} interfaces matched with requested interfaces and will be used by PyStageLinQ:" )
77+ for iface in self .target_interfaces :
78+ logger .info (f" - { iface .name } : { iface .addr_str } " )
79+
80+
81+
7682
7783 def send_discovery_frame (self , discovery_frame ):
7884 for interface in self .target_interfaces :
@@ -214,9 +220,11 @@ async def _discover_stagelinq_device(self, host_ip, timeout=10):
214220 # Create socket
215221 discover_socket = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
216222
223+ bind_ip = "" if os .name == "posix" else host_ip
224+
217225 try :
218226 discover_socket .bind (
219- (host_ip , self .StageLinQ_discovery_port )
227+ (bind_ip , self .StageLinQ_discovery_port )
220228 ) # bind socket to broadcast
221229 except Exception as e :
222230 # Cannot bind to socket, check if IP is correct and link is up
0 commit comments