Newbie question: what is causing my socket error?

G

google account

Hiya! any guidance would be greatly appreciated.

I wrote a little mailer script to be used by otehr tasks to email logs
to specific mail accounts. I first wrote it for use on a RH8 server
and this works great. I recently had cause to use it on a new Fedora
Core2 box, and I get the following error when I run it:

Traceback (most recent call last):
File "./mailer", line 12, in ?
mail_server = smtplib.SMTP('melmail')
File "/usr/lib/python2.3/smtplib.py", line 254, in __init__
addr = socket.gethostbyname(socket.gethostname())
socket.gaierror: (-2, 'Name or service not known')


Looking at it, this seems to me to be indicating that DNS is failing,
so I rewrote it with an IP address of the mail server instead. Now I
get the same error but with the IP Address listed instead:


Traceback (most recent call last):
File "./old.mailer", line 10, in ?
mail_server = smtplib.SMTP('172.30.30.240')
File "/usr/lib/python2.3/smtplib.py", line 254, in __init__
addr = socket.gethostbyname(socket.gethostname())
socket.gaierror: (-2, 'Name or service not known')


The script is simple to the nth degree of newbieness, I'd say.



#!/usr/bin/python

from email.MIMEText import MIMEText
import smtplib
import sys

email = MIMEText(sys.argv[1])
email['Subject'] = "Log Files R Us"

mail_server = smtplib.SMTP('172.30.30.240')
mail_server.sendmail('txt2mail',
sys.argv[2:],
email.as_string() )
mail_server.quit()




The code in the library at line 254 is

addr = socket.gethostbyname(socket.gethostname())

which would kinda support my theory about name resolution, but not
help me resolve the error.

thanks!!!
 
P

Peter Hansen

google said:
The code in the library at line 254 is

addr = socket.gethostbyname(socket.gethostname())

which would kinda support my theory about name resolution, but not
help me resolve the error.

Can you just try executing those two expressions individually,
starting with socket.gethostname(), at the interactive prompt,
to see what you get?

This doesn't seem to have anything to do with smtplib, so don't
complicate your life with it...
'192.168.0.197'

on my machine....

-Peter
 
J

Jeff Epler

socket.gethostname() should return the fully-qualified name of the
machine where the Python script is executing. This name would be used,
I think, in the HELO step of the SMTP protocol.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAzuRKJd01MZaTXX0RArOGAJ9s7hCOo2ep/EQ3I9mOLjo63HVtGgCaApA6
emGHnLnJtVUmjTbNwg99Gdc=
=AsDc
-----END PGP SIGNATURE-----
 
G

google account

[email protected] (google account) wrote in message news: said:
The code in the library at line 254 is

addr = socket.gethostbyname(socket.gethostname())

which would kinda support my theory about name resolution, but not
help me resolve the error.


Turns out I was being pretty brickheaded... It wasn't complaining
about not being able to resolve the mail server. It was complaining
about not being able to resolve the host name of the box it was
sitting on.

It is actually mentioned in the sockets documentation somewhere that
this is a requirement.

On my linuxbox, I put an entry for the hostname in the /etc/hosts
file, and then it worked like a bought one.

Thanks for looking! Sorry to trouble you.

Hope this post saves someone some time someday.

^_^

cha!
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top