Failed Regression Test: What socket.gethostname() is supposed toreturn?

L

Lie Ryan

In my laptop, socket.gethostname() returned my username, and causing one
of python's "make test" regression test to error (test_socket):

======================================================================
ERROR: testSockName (test.test_socket.GeneralModuleTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File
"/var/tmp/portage/dev-lang/python-2.5.4-r3/work/Python-2.5.4/Lib/test/test_socket.py",
line 456, in testSockName
my_ip_addr = socket.gethostbyname(socket.gethostname())
gaierror: (-2, 'Name or service not known')

----------

since on my system socket.gethostname() returns 'lieryan', and since
socket.gethostbyname('lieryan') does not resolve to anything; the test
becomes an error.

My system is Gentoo, but I think this also happened on Ubuntu (still on
this laptop). The trunk failed in similar manner.

Do I have a misconfigured system or is the test faulty?

For convenience, the relevant test code (taken from trunk):

=======================
def testSockName(self):
# Testing getsockname()
port = self._get_unused_port()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("0.0.0.0", port))
name = sock.getsockname()
# XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate
# it reasonable to get the host's addr in addition to 0.0.0.0.
# At least for eCos. This is required for the S/390 to pass.
my_ip_addr = socket.gethostbyname(socket.gethostname())
self.assertTrue(name[0] in ("0.0.0.0", my_ip_addr), '%s invalid' %
name[0])
self.assertEqual(name[1], port)
=======================


lieryan@lieryan ~/Desktop/pythontrunk/trunk $ ./python -m test.regrtest
test_socket
Could not find '/home/lieryan/Desktop/pythontrunk/trunk/Lib/test' in
sys.path to remove it
test_socket
test test_socket failed -- Traceback (most recent call last):
File
"/home/lieryan/Desktop/pythontrunk/trunk/Lib/test/test_socket.py", line
493, in testSockName
my_ip_addr = socket.gethostbyname(socket.gethostname())
gaierror: [Errno -2] Name or service not known

1 test failed:
test_socket

I tracked the code for socket.gethostname() and socket.gethostbyname()
and found that they are simply a wrapper for gethostname() from #import
<unistd.h> (http://linux.die.net/man/2/gethostname) and gethostbyname()
from #import <netdb.h> (http://linux.die.net/man/3/gethostbyname). A
simple test in C found that the C's equivalent to
gethostbyname(gethostname()) returns a null pointer (used to indicate
error, per documentation).

So, the question is: what is socket.gethostname() is supposed to return
that will be a valid argument for socket.gethostbyname()?

PS: I found an MSDN article by Microsoft stating that
gethostbyname(gethostname) is guaranteed to always succeed
(http://msdn.microsoft.com/en-us/library/ms738527(VS.85).aspx); is this
guarantee also true in linux?
 
N

Nobody

since on my system socket.gethostname() returns 'lieryan', and since
socket.gethostbyname('lieryan') does not resolve to anything; the test
becomes an error.

My system is Gentoo, but I think this also happened on Ubuntu (still on
this laptop). The trunk failed in similar manner.

Do I have a misconfigured system or is the test faulty?

Misconfigured system.
I tracked the code for socket.gethostname() and socket.gethostbyname()
and found that they are simply a wrapper for gethostname() from #import
<unistd.h> (http://linux.die.net/man/2/gethostname) and gethostbyname()
from #import <netdb.h> (http://linux.die.net/man/3/gethostbyname). A
simple test in C found that the C's equivalent to
gethostbyname(gethostname()) returns a null pointer (used to indicate
error, per documentation).

So, the question is: what is socket.gethostname() is supposed to return
that will be a valid argument for socket.gethostbyname()?

gethostname() returns the hostname.

gethostbyname() returns some information about the host (specifically,
aliases and IP addresses) obtained by platform-specific means (which
normally includes /etc/hosts and DNS, and may also include NIS, LDAP, and
others; see the nsswitch.conf manpage for more details).

Python's socket.gethostname() simply returns the first IP address;
socket.gethostbyname_ex() returns all of the information which libc's
gethostbyname() provides.

In order for gethostbyname() to work, the host must be listed in one of
the databases which it uses. The simplest way to achieve this is to add an
entry to /etc/hosts, e.g.:

192.168.0.2 lieryan.yourdomain.com lieryan

or just:

192.168.0.2 lieryan
PS: I found an MSDN article by Microsoft stating that
gethostbyname(gethostname) is guaranteed to always succeed
(http://msdn.microsoft.com/en-us/library/ms738527(VS.85).aspx); is this
guarantee also true in linux?

No.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top