Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sockets for finding ip addresses. Closes #227 #228

Merged
merged 3 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions chimerapy/engine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

# Third-party
from tqdm import tqdm
import ifaddr
umesh-timalsina marked this conversation as resolved.
Show resolved Hide resolved

# Internal
from chimerapy.engine import _logger
Expand Down Expand Up @@ -145,20 +144,22 @@ def get_open_port(start_port: int) -> socket.socket:


def get_ip_address() -> str:

# Get all the network adapters
adapters = ifaddr.get_adapters()

# Iterate through the adapters to find a suitable IP
for adapter in adapters:
for ip in adapter.ips:
# Check if it's an IPv4 address and not the local address
if isinstance(ip.ip, str) and ip.ip != "127.0.0.1":
return ip.ip

# Return the local address if no suitable IP is found
logger.warning("ChimeraPy-Engine: Couldn't find connected network, using 127.0.0.1")
return "127.0.0.1"
"""Get the IP address of the current machine."""

# https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)

try:
# doesn't even have to be reachable
s.connect(("10.254.254.254", 1))
ip = str(s.getsockname()[0])
except: # noqa E722
ip = "127.0.0.1"
finally:
s.close()

return ip


def create_payload(
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ classifiers = [
dependencies = [
'networkx',
'dill',
'ifaddr',
'matplotlib',
'multiprocess',
'opencv-python',
Expand Down
Loading