Socket - gaierror

Steve Holden steve at
Tue Aug 28 04:22:47 CEST 2007

half.italian at wrote:
> On Aug 27, 12:47 pm, half.ital... at wrote:
>> On Aug 27, 12:32 pm, Larry Bates < at> wrote:
>>> half.ital... at wrote:
>>>> Hi all,
>>>> I'm having trouble with the socket module resolving a hostname.  It
>>>> seems like this is a system level problem, but I'm not even sure where
>>>> to start.  I can ping the smtp server by name and IP, but when
>>>> smtp.SMTP("theHost") tries to get the hostname, it keeps giving me the
>>>> following error:
>>>>   File "bin/", line 123, in notify
>>>>     smtp = smtplib.SMTP("theHost")
>>>>   File "/usr/lib/python2.4/", line 255, in __init__
>>>>     addr = socket.gethostbyname(socket.gethostname())
>>>> gaierror: (-2, 'Name or service not known')
>>>> I tried changing to a different smtp server, using an ip instead of a
>>>> host name.  I could ping both boxes by name oor IP.
>>>> any ideas?
>>>> ~Sean
>>> The specific error shown is a DNS resolution problem.  Based on the
>>> URL "theHost" smtplib can't resolve to an IP address (which is what
>>> socket.gethostbyname does).  You don't say what happened when you changed to ip,
>>> but I suspect it is a different error or some other problem.  Maybe a firewall
>>> issue (port 25 not open?), but I'm just guessing.
>>> -Larry
>> Changing it to IP gives me the same exact error...
>>   File "bin/", line 123, in notify
>>     smtp = smtplib.SMTP("XXX.XXX.XXX.XXX")
>>   File "/usr/lib/python2.4/", line 255, in __init__
>>     addr = socket.gethostbyname(socket.gethostname())
>> gaierror: (-2, 'Name or service not known')
>> Looks like the smtp port is closed on the client machine...doh Should
>> have gotten to that!
>> Thank you.
>> ~Sean
> I take it back, that wasn't the solution.  I got it to work by
> haacking smtplib with a static host instead of the
> socket.gethostbyname(socket.gethostname()) call...but if I leave that
> in there I might get shot one day.  What could cause the shell to be
> able to resolve the addresses properly, but not python?
> nnsswitch.conf ?
> I don't know enough about what's going on in the background.  Any
> other ideas? Or should I try this on a unix board?
Well the first thing to do is call socket.gethostname() and see what it 
returns, then call socket.gethostbyname() on the result. Once you know 
what's failing we might be able to help.

Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd 
Skype: holdenweb
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

More information about the Python-list mailing list