views:

24

answers:

1

I'm having a problem resolving a hostname using python's (2.6.2) socket class.

From the shell I'm able to ping the hostname, and also resolve the hostname using the host command:

host myhostname.mydomain.com

When I attempt to resolve it with python, a socket.herror exception is raised with the message "[Errno 1] Unknown host"

socket.gethostbyaddr("myhostname.mydomain.com")

I've recently added the nameservers to resolv.conf, perhaps i need to restart something for python to see these updates?

Any ideas?

+5  A: 

You need to use gethostbyname, not gethostbyaddr (which does reverse lookup).

>>> socket.gethostbyname('car.spillville.com')
'209.20.76.192'
>>> socket.gethostbyaddr('209.20.76.192')
('car.spillville.com', [], ['209.20.76.192'])
Chris Jester-Young
That works a charm - thanks!
Ash Kim