This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: socket gethostbyaddr returns IPv6 names for 127.0.0.1
Type: behavior Stage:
Components: Versions: Python 3.4, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: carbonin, r.david.murray
Priority: normal Keywords:

Created on 2016-10-13 15:12 by carbonin, last changed 2022-04-11 14:58 by admin.

Messages (2)
msg278580 - (view) Author: Nick Carboni (carbonin) Date: 2016-10-13 15:12
socket.gethostbyaddr seems to be equating loopback addresses regardless of IP protocol version.

In both versions tested (2.7.5 and 3.4.3) the ordering of the entries in my /etc/hosts file determines the result I get, rather than what address I'm querying for.

For example:

/etc/hosts:

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

result:

>>> import socket
>>> socket.gethostbyaddr("127.0.0.1")
('localhost', ['localhost.localdomain', 'localhost6', 'localhost6.localdomain6'], ['127.0.0.1'])

Then if I change the ordering of the entries in /etc/hosts as follows:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

result:

>>> import socket
>>> socket.gethostbyaddr("127.0.0.1")
('localhost', ['localhost.localdomain', 'localhost4', 'localhost4.localdomain4'], ['127.0.0.1'])

I would expect gethostbyaddr to return only the hostnames associated with the given address regardless of the ordering of the entries in /etc/hosts.
msg278582 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-10-13 15:24
I believe that you will find that the same thing happens if you call gethostbyaddr from C.  So this either isn't a bug, or it isn't a bug in Python :)

(Correct me if I'm wrong; I don't have time to actually test it myself, but gethostbyaddr is a fairly thing wrapper.)
History
Date User Action Args
2022-04-11 14:58:38adminsetgithub: 72617
2016-10-13 15:24:53r.david.murraysetnosy: + r.david.murray
messages: + msg278582
2016-10-13 15:12:28carbonincreate