From 6882143b1c9d3e8c734fff2709269b7951c2c6c7 Mon Sep 17 00:00:00 2001 From: Yeguang Xue <2394844+yeguang-xue@users.noreply.github.com> Date: Fri, 11 Jul 2025 12:41:26 -0700 Subject: [PATCH 1/7] Fix socket.gethostbyaddr(ip) error when no reverse DNS available. For ip addresses without reverse DNS, socket.gethostbyaddr(ip) will fail. We can simply use ip address for hostname display. --- src/ansys/mapdl/core/mapdl_grpc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index be3cc7e458..fd1399d19c 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -383,7 +383,7 @@ def __init__( self._hostname = ( "localhost" if ip in ["127.0.0.1", "127.0.1.1", "localhost"] - else socket.gethostbyaddr(ip)[0] + else ip ) check_valid_ip(ip) From e3b8d74ecd8d76087564c196273da1acdea2b4fe Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 11 Jul 2025 19:43:23 +0000 Subject: [PATCH 2/7] ci: auto fixes from pre-commit.com hooks. for more information, see https://pre-commit.ci --- src/ansys/mapdl/core/mapdl_grpc.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index fd1399d19c..620e3f001e 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -381,9 +381,7 @@ def __init__( else: # it is an IP self._hostname = ( - "localhost" - if ip in ["127.0.0.1", "127.0.1.1", "localhost"] - else ip + "localhost" if ip in ["127.0.0.1", "127.0.1.1", "localhost"] else ip ) check_valid_ip(ip) From 0d6d3e1502632d12037e70b908113fe6ca90257d Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Mon, 14 Jul 2025 07:38:27 +0000 Subject: [PATCH 3/7] chore: adding changelog file 4095.miscellaneous.md [dependabot-skip] --- doc/changelog.d/4095.miscellaneous.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/4095.miscellaneous.md diff --git a/doc/changelog.d/4095.miscellaneous.md b/doc/changelog.d/4095.miscellaneous.md new file mode 100644 index 0000000000..a40c38f0f5 --- /dev/null +++ b/doc/changelog.d/4095.miscellaneous.md @@ -0,0 +1 @@ +Migrated (pr 4094): fix socket.gethostbyaddr(ip) error when no reverse dns available. \ No newline at end of file From cf71381c28e49565b2064631763a174ec01edd21 Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Mon, 14 Jul 2025 07:55:14 +0000 Subject: [PATCH 4/7] chore: adding changelog file 4095.miscellaneous.md [dependabot-skip] --- doc/changelog.d/4095.miscellaneous.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changelog.d/4095.miscellaneous.md b/doc/changelog.d/4095.miscellaneous.md index a40c38f0f5..37b1972b1b 100644 --- a/doc/changelog.d/4095.miscellaneous.md +++ b/doc/changelog.d/4095.miscellaneous.md @@ -1 +1 @@ -Migrated (pr 4094): fix socket.gethostbyaddr(ip) error when no reverse dns available. \ No newline at end of file +Fix: (migrated pr 4094): fix socket.gethostbyaddr(ip) error when no reverse dns available. \ No newline at end of file From d7b328cebd35f78540d56921f388b5a2d9477127 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 14 Jul 2025 10:10:08 +0200 Subject: [PATCH 5/7] refactor: moving to misc getting ip and hostname. Also adding a try/except clause. --- src/ansys/mapdl/core/mapdl_grpc.py | 15 +++------------ src/ansys/mapdl/core/misc.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index 620e3f001e..9202e98798 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -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. @@ -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, @@ -373,16 +372,8 @@ 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 ip - ) + # setting hostname and ip + ip, hostname = get_ip_hostname(ip) check_valid_ip(ip) self._ip: str = ip diff --git a/src/ansys/mapdl/core/misc.py b/src/ansys/mapdl/core/misc.py index d81f4b7202..39cc4a2e43 100644 --- a/src/ansys/mapdl/core/misc.py +++ b/src/ansys/mapdl/core/misc.py @@ -666,3 +666,23 @@ def deco(f): return f return deco + + +def get_ip_hostname(ip): + """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: + # If the IP address does not resolve to a hostname, use the IP + hostname = ip + + return ip, hostname From c8502e9627e18263c1ec5af1a31a8b37bee87457 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 14 Jul 2025 10:11:22 +0200 Subject: [PATCH 6/7] refactor: add type hint to get_ip_hostname function --- src/ansys/mapdl/core/misc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ansys/mapdl/core/misc.py b/src/ansys/mapdl/core/misc.py index 39cc4a2e43..c7ae2623cc 100644 --- a/src/ansys/mapdl/core/misc.py +++ b/src/ansys/mapdl/core/misc.py @@ -668,7 +668,7 @@ def deco(f): return deco -def get_ip_hostname(ip): +def get_ip_hostname(ip: str) -> Tuple[str, str]: """Get ip and hostname""" if not only_numbers_and_dots(ip): # it is a hostname From f99fd824260a279e37a4b2e74da82a0990745a32 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 14 Jul 2025 12:23:17 +0200 Subject: [PATCH 7/7] feat: store hostname in MapdlGrpc class after retrieving IP and hostname --- src/ansys/mapdl/core/mapdl_grpc.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index 9202e98798..76eaf53ad9 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -374,6 +374,7 @@ def __init__( # setting hostname and ip ip, hostname = get_ip_hostname(ip) + self._hostname = hostname check_valid_ip(ip) self._ip: str = ip