classification
Title: socket.gethostbyname, getaddrinfo etc broken on MacOS 10.12
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: James Crowther, ned.deily, ssbarnea
Priority: normal Keywords:

Created on 2017-03-02 23:36 by James Crowther, last changed 2018-11-04 17:29 by ssbarnea. This issue is now closed.

Messages (8)
msg288840 - (view) Author: James Crowther (James Crowther) Date: 2017-03-02 23:36
Currently I can't use socket to resolve host names to IP addresses. This is something critical to mine as well as other applications that run over networks. 
When I attempt to do the following:

import socket
socket.getaddrinfo(hostname, None)

or 

socket.gethostbyname(hostname)

I get 

socket.gaierror: [Errno 8] nodename nor servename provided, or not known.

This works perfectly on both linux kubuntu 16.0. and windows 7,10. 

Seems that the introduction of Yosemite might be the point at which this broke by doing a simple google search for "macos socket.gethostbyname gaierror".
msg288842 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2017-03-03 00:27
What value are you using for hostname?
msg288843 - (view) Author: James Crowther (James Crowther) Date: 2017-03-03 00:30
Hi Ned, 
	Doesn’t seem to matter, I can try my local host name given by;

socket.gethostname()

or I can try another host on the network, same result. 

If I do the exact same operation using the same python version on windows or linux, then I get the expected result from gethostbyname which is the ip address of the host. Also on macOS 10.6.8 this still works, using the same version of python.

Happy to give more details if you need.

James
> On 3 Mar 2017, at 11:27 am, Ned Deily <report@bugs.python.org> wrote:
> 
> 
> Ned Deily added the comment:
> 
> What value are you using for hostname?
> 
> ----------
> nosy: +ned.deily
> 
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue29705>
> _______________________________________
msg288844 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2017-03-03 00:41
Sorry, I can't reproduce that behavior with my macOS 10.12, 10.11, or 10.10 systems if the hostname is a valid string including "localhost".  If the string is empty or hostname is None, then I see Errno 8.

$ /usr/local/bin/python3.6
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> hostname = 'python.org'
>>> socket.getaddrinfo(hostname, None)
[(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('23.253.135.79', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('23.253.135.79', 0)), (<AddressFamily.AF_INET6: 30>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('2001:4802:7901::e60a:1375:0:6', 0, 0, 0)), (<AddressFamily.AF_INET6: 30>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('2001:4802:7901::e60a:1375:0:6', 0, 0, 0))]
>>> hostname = "localhost"
>>> socket.getaddrinfo(hostname, None)
[(<AddressFamily.AF_INET6: 30>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('::1', 0, 0, 0)), (<AddressFamily.AF_INET6: 30>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('::1', 0, 0, 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('127.0.0.1', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('127.0.0.1', 0))]
>>> hostname = ""
>>> socket.getaddrinfo(hostname, None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 743, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

Perhaps there is something different about your hostname configuration on your Mac?  macOS has some unusual ways of configuring the hostname resolutions.  I don't have time at the moment to investigate further.  But Python's socket module interfaces tend to be very thin wrappers around the underlying OS provided APIs.  Perhaps you could test with a small C program that does the equivalent.
msg288845 - (view) Author: James Crowther (James Crowther) Date: 2017-03-03 00:53
Hi Ned, 
	Currently running 10.12.3. and output is as follows

Jamess-MacBook-pro:crowdrender_repository jamesmac$ python3.5
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> hostname = socket.gethostname()
>>> socket.getaddrinfo(hostname, None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 732, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
>>> 

This is a commonly prescribed method of resolving the ip address of your own machine on a LAN, also if I try another machines hostname which is on the local network, I get the same result. 

As for the experiments you tried, these do work, but that is not what I want to use the socket library for, I want to query the IP address of hosts on the LAN, localhost is no use to me, its just the loop back address.

I have a windows machine on my network and its hostname is James-PC, if i do

socket.getaddrinfo(“James-PC”)

I get the same error. This works if I reboot the mac (the same one that is having the issues) into linux and run the exact same command. I get the expected output from getaddrinfo. 

So, how come this approach doesn’t work on my mac? Also why are others having the same issue with Yosemite and sierra, this is not just me that is reporting this odd behaviour too. I’ve seen it in other forums.

Kind Regards

James

> On 3 Mar 2017, at 11:41 am, Ned Deily <report@bugs.python.org> wrote:
> 
> 
> Ned Deily added the comment:
> 
> Sorry, I can't reproduce that behavior with my macOS 10.12, 10.11, or 10.10 systems if the hostname is a valid string including "localhost".  If the string is empty or hostname is None, then I see Errno 8.
> 
> $ /usr/local/bin/python3.6
> Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13)
> [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import socket
>>>> hostname = 'python.org'
>>>> socket.getaddrinfo(hostname, None)
> [(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('23.253.135.79', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('23.253.135.79', 0)), (<AddressFamily.AF_INET6: 30>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('2001:4802:7901::e60a:1375:0:6', 0, 0, 0)), (<AddressFamily.AF_INET6: 30>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('2001:4802:7901::e60a:1375:0:6', 0, 0, 0))]
>>>> hostname = "localhost"
>>>> socket.getaddrinfo(hostname, None)
> [(<AddressFamily.AF_INET6: 30>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('::1', 0, 0, 0)), (<AddressFamily.AF_INET6: 30>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('::1', 0, 0, 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('127.0.0.1', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('127.0.0.1', 0))]
>>>> hostname = ""
>>>> socket.getaddrinfo(hostname, None)
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 743, in getaddrinfo
>    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
> socket.gaierror: [Errno 8] nodename nor servname provided, or not known
> 
> Perhaps there is something different about your hostname configuration on your Mac?  macOS has some unusual ways of configuring the hostname resolutions.  I don't have time at the moment to investigate further.  But Python's socket module interfaces tend to be very thin wrappers around the underlying OS provided APIs.  Perhaps you could test with a small C program that does the equivalent.
> 
> ----------
> 
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue29705>
> _______________________________________
msg288846 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2017-03-03 02:45
James, I'm sorry I didn't show it but using socket.gethostname() as the source of hostname works just fine, too.

import socket
>>> hostname = socket.gethostname()
>>> hostname
'harj.local'
>>> socket.getaddrinfo(hostname, None)
[(<AddressFamily.AF_INET6: 30>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('fe80::8d8:1de3:dfa:e34c%en1', 0, 0, 5)), (<AddressFamily.AF_INET6: 30>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('fe80::8d8:1de3:dfa:e34c%en1', 0, 0, 5)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('10.0.1.7', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('10.0.1.7', 0))]

I don't know what to tell you other than the behavior you are seeing is almost certainly not a Python issue.  There are many other ways to explore host names, like using the "host" command line utility or "netstat".  But that is all beyond the scope of this bug tracker.  If you need more assistance, perhaps ask on one of the StackExchange forums or Apple lists.  I'm going to close this issue; if you are able to isolate what appears to be a Python issue here, please feel free to re-open this.  Good luck!
msg288847 - (view) Author: James Crowther (James Crowther) Date: 2017-03-03 02:49
Hi Ned, 
	Thats ok, thanks! I’m going to try it on another machine, its really strange, I’m wondering what I might have done on my mac to cause it to flake out like this. Will do some more testing with other macs running 10.12 and see if they have the same behaviour, by the way, thanks for running that last test as it shows that this call is actually working on some systems. I really need to know the reason why its not working on mine, I have about 120 users and need to be sure that they’re not going to have our next build break if we rely on this call. 

Thanks :D

James

> On 3 Mar 2017, at 1:45 pm, Ned Deily <report@bugs.python.org> wrote:
> 
> 
> Ned Deily added the comment:
> 
> James, I'm sorry I didn't show it but using socket.gethostname() as the source of hostname works just fine, too.
> 
> import socket
>>>> hostname = socket.gethostname()
>>>> hostname
> 'harj.local'
>>>> socket.getaddrinfo(hostname, None)
> [(<AddressFamily.AF_INET6: 30>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('fe80::8d8:1de3:dfa:e34c%en1', 0, 0, 5)), (<AddressFamily.AF_INET6: 30>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('fe80::8d8:1de3:dfa:e34c%en1', 0, 0, 5)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('10.0.1.7', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('10.0.1.7', 0))]
> 
> I don't know what to tell you other than the behavior you are seeing is almost certainly not a Python issue.  There are many other ways to explore host names, like using the "host" command line utility or "netstat".  But that is all beyond the scope of this bug tracker.  If you need more assistance, perhaps ask on one of the StackExchange forums or Apple lists.  I'm going to close this issue; if you are able to isolate what appears to be a Python issue here, please feel free to re-open this.  Good luck!
> 
> ----------
> resolution:  -> works for me
> stage:  -> resolved
> status: open -> closed
> 
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue29705>
> _______________________________________
msg329242 - (view) Author: Sorin Sbarnea (ssbarnea) * Date: 2018-11-04 17:29
It seems that the bug is still present. There is some additional
information on the new report https://bugs.python.org/issue35164
History
Date User Action Args
2018-11-04 17:29:22ssbarneasetnosy: + ssbarnea
messages: + msg329242
2017-03-03 02:49:20James Crowthersetmessages: + msg288847
2017-03-03 02:45:56ned.deilysetstatus: open -> closed
resolution: works for me
messages: + msg288846

stage: resolved
2017-03-03 00:53:09James Crowthersetmessages: + msg288845
2017-03-03 00:41:13ned.deilysetmessages: + msg288844
2017-03-03 00:30:09James Crowthersetmessages: + msg288843
2017-03-03 00:27:52ned.deilysetnosy: + ned.deily
messages: + msg288842
2017-03-02 23:36:55James Crowthercreate