Skip to content

Commit 12be23c

Browse files
authored
pythongh-100374: Fixed a bug in socket.getfqdn() (pythongh-100375)
1 parent a7715cc commit 12be23c

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

Lib/socket.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -785,11 +785,11 @@ def getfqdn(name=''):
785785
786786
First the hostname returned by gethostbyaddr() is checked, then
787787
possibly existing aliases. In case no FQDN is available and `name`
788-
was given, it is returned unchanged. If `name` was empty or '0.0.0.0',
788+
was given, it is returned unchanged. If `name` was empty, '0.0.0.0' or '::',
789789
hostname from gethostname() is returned.
790790
"""
791791
name = name.strip()
792-
if not name or name == '0.0.0.0':
792+
if not name or name in ('0.0.0.0', '::'):
793793
name = gethostname()
794794
try:
795795
hostname, aliases, ipaddrs = gethostbyaddr(name)

Lib/test/test_socket.py

+4
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,10 @@ def test_getaddrinfo_ipv6_basic(self):
17691769
)
17701770
self.assertEqual(sockaddr, ('ff02::1de:c0:face:8d', 1234, 0, 0))
17711771

1772+
def test_getfqdn_filter_localhost(self):
1773+
self.assertEqual(socket.getfqdn(), socket.getfqdn("0.0.0.0"))
1774+
self.assertEqual(socket.getfqdn(), socket.getfqdn("::"))
1775+
17721776
@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.')
17731777
@unittest.skipIf(sys.platform == 'win32', 'does not work on Windows')
17741778
@unittest.skipIf(AIX, 'Symbolic scope id does not work')
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)