Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Al-Saffar committed Oct 11, 2024
2 parents eabcce6 + d80a3c1 commit 0cfcaef
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions myresources/crocodile/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,16 @@ def get_network_addresses() -> NetworkAddresses:
# elif hex_format: return hex(mac)
# else: return mac
import socket
try: local_ip_v4 = socket.gethostbyname(socket.gethostname() + ".local") # without .local, in linux machines, '/etc/hosts' file content, you have an IP address mapping with '127.0.1.1' to your hostname
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# Doesn't need to be reachable
s.connect(('8.8.8.8', 1))
local_ip_v4 = s.getsockname()[0]
except Exception:
print("Warning: Could not get local_ip_v4. This is probably because you are running a WSL instance") # TODO find a way to get the local_ip_v4 in WSL
local_ip_v4 = socket.gethostbyname(socket.gethostname())
# local_ip_v4 = '127.0.0.1'
finally:
s.close()
# _addresses: TypeAlias = Literal['subnet_mask', 'mac_address', 'local_ip_v4', 'default_gateway', 'public_ip_v4']
res = NetworkAddresses(subnet_mask=None, mac_address=mac_address, local_ip_v4=local_ip_v4, default_gateway=None, public_ip_v4=P('https://api.ipify.org').download_to_memory().text)
return res
Expand Down

0 comments on commit 0cfcaef

Please sign in to comment.