Skip to content

Commit 7f24056

Browse files
gh-100374: Fixed a bug in socket.getfqdn() (gh-100375)
(cherry picked from commit 12be23c) Co-authored-by: Dominic Socular <[email protected]>
1 parent 591365c commit 7f24056

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

Lib/socket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,11 +783,11 @@ def getfqdn(name=''):
783783
784784
First the hostname returned by gethostbyaddr() is checked, then
785785
possibly existing aliases. In case no FQDN is available and `name`
786-
was given, it is returned unchanged. If `name` was empty or '0.0.0.0',
786+
was given, it is returned unchanged. If `name` was empty, '0.0.0.0' or '::',
787787
hostname from gethostname() is returned.
788788
"""
789789
name = name.strip()
790-
if not name or name == '0.0.0.0':
790+
if not name or name in ('0.0.0.0', '::'):
791791
name = gethostname()
792792
try:
793793
hostname, aliases, ipaddrs = gethostbyaddr(name)

Lib/test/test_socket.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,10 @@ def test_getaddrinfo_ipv6_basic(self):
17431743
)
17441744
self.assertEqual(sockaddr, ('ff02::1de:c0:face:8d', 1234, 0, 0))
17451745

1746+
def test_getfqdn_filter_localhost(self):
1747+
self.assertEqual(socket.getfqdn(), socket.getfqdn("0.0.0.0"))
1748+
self.assertEqual(socket.getfqdn(), socket.getfqdn("::"))
1749+
17461750
@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.')
17471751
@unittest.skipIf(sys.platform == 'win32', 'does not work on Windows')
17481752
@unittest.skipIf(AIX, 'Symbolic scope id does not work')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix incorrect result and delay in :func:`socket.getfqdn`. Patch by Dominic Socular.

0 commit comments

Comments
 (0)