Skip to content

fix: (migrated PR 4094): fix socket.gethostbyaddr(ip) error when no reverse DNS available. #4095

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

Merged
merged 7 commits into from
Jul 14, 2025
1 change: 1 addition & 0 deletions doc/changelog.d/4095.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix: (migrated pr 4094): fix socket.gethostbyaddr(ip) error when no reverse dns available.
18 changes: 4 additions & 14 deletions src/ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import pathlib
import re
import shutil
import socket

# Subprocess is needed to start the backend. But
# the input is controlled by the library. Excluding bandit check.
Expand Down Expand Up @@ -87,8 +86,8 @@
from ansys.mapdl.core.mapdl_types import KwargDict, MapdlFloat, MapdlInt
from ansys.mapdl.core.misc import (
check_valid_ip,
get_ip_hostname,
last_created,
only_numbers_and_dots,
random_string,
run_as,
supress_logging,
Expand Down Expand Up @@ -373,18 +372,9 @@ def __init__(
if ip is None:
ip = start_parm.pop("ip", None) or "127.0.0.1"

# setting hostname
if not only_numbers_and_dots(ip):
# it is a hostname
self._hostname = ip
ip = socket.gethostbyname(ip)
else:
# it is an IP
self._hostname = (
"localhost"
if ip in ["127.0.0.1", "127.0.1.1", "localhost"]
else socket.gethostbyaddr(ip)[0]
)
# setting hostname and ip
ip, hostname = get_ip_hostname(ip)
self._hostname = hostname

check_valid_ip(ip)
self._ip: str = ip
Expand Down
20 changes: 20 additions & 0 deletions src/ansys/mapdl/core/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,3 +666,23 @@
return f

return deco


def get_ip_hostname(ip: str) -> Tuple[str, str]:
"""Get ip and hostname"""
if not only_numbers_and_dots(ip):
# it is a hostname
hostname = ip
ip = socket.gethostbyname(ip)
else:
# it is an IP
if ip in ["127.0.0.1", "127.0.1.1", "localhost"]:
hostname = "localhost"
else:
try:
hostname = socket.gethostbyaddr(ip)[0]
except OSError:

Check warning on line 684 in src/ansys/mapdl/core/misc.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/misc.py#L684

Added line #L684 was not covered by tests
# If the IP address does not resolve to a hostname, use the IP
hostname = ip

Check warning on line 686 in src/ansys/mapdl/core/misc.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/misc.py#L686

Added line #L686 was not covered by tests

return ip, hostname
Loading