Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Newbie question: what is causing my socket error?

Reply
Thread Tools

Newbie question: what is causing my socket error?

 
 
google account
Guest
Posts: n/a
 
      06-15-2004
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!!!
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      06-15-2004
google account wrote:
> 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...

>>> import socket
>>> socket.gethostname()

'monolith'
>>> socket.gethostbyname(_)

'192.168.0.197'

on my machine....

-Peter
 
Reply With Quote
 
 
 
 
Jeff Epler
Guest
Posts: n/a
 
      06-15-2004
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-----

 
Reply With Quote
 
google account
Guest
Posts: n/a
 
      06-15-2004
(google account) wrote in message news:<. com>...
<snipped problem description>
>
> 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57