Skip to content

Commit 125d1b1

Browse files
committed
fix get hostname, if something is tinkering with the loopback interface on Windows - for instance 'Solid Works 3DExperience'
1 parent c4d791a commit 125d1b1

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

lib_platform/lib_platform.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ def get_hostname() -> str:
1717
1818
>>> result = get_hostname()
1919
>>> assert len(result) > 1
20-
2120
"""
2221

23-
if get_is_platform_windows_wine(): # for wine get hostname not via IP Adress - would give name of the host
22+
if get_is_platform_windows_wine(): # for wine get hostname not via IP Adress - that would give name of the linux host
2423
# noinspection PyBroadException
2524
try:
2625
result_wine_reg = lib_registry.Registry().get_value(key=r'HKLM\System\CurrentControlSet\Control\ComputerName', value_name='ComputerName')
@@ -34,7 +33,7 @@ def get_hostname() -> str:
3433
_hostname = result_wine_env
3534

3635
elif get_is_platform_windows():
37-
_hostname = socket.getfqdn()
36+
_hostname = _get_fqdn_by_hostname()
3837
else:
3938
# this one failed on the first call sometimes - use now getfqdn() supports both IPv4 and IPv6. - and sometimes give WRONG HOSTNAME
4039
# _hostname = socket.gethostbyaddr(socket.gethostname())[0]
@@ -49,6 +48,22 @@ def get_hostname() -> str:
4948
return str(_hostname)
5049

5150

51+
def _get_fqdn_by_hostname() -> str:
52+
"""
53+
Returns fqdn by hostname
54+
if You use just socket.getfqdn(), it will return 'dslauncher.3ds.com' if Solid Works 3DExperience is installed.
55+
this is because they tinker with the loopback address
56+
therefore we get hostname --> ip adress --> fqdn
57+
58+
>>> assert _get_fqdn_by_hostname()
59+
60+
"""
61+
_hostname_short = socket.gethostname()
62+
_ip_address = socket.gethostbyname(_hostname_short)
63+
_fqdn = socket.getfqdn(name=_ip_address)
64+
return _fqdn
65+
66+
5267
def get_hostname_short() -> str:
5368
"""
5469
Returns hostname lowercase without domain part

0 commit comments

Comments
 (0)