Newbie question: what is causing my socket error?

Discussion in 'Python' started by google account, Jun 15, 2004.

  1. 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!!!
     
    google account, Jun 15, 2004
    #1
    1. Advertising

  2. google account

    Peter Hansen Guest

    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
     
    Peter Hansen, Jun 15, 2004
    #2
    1. Advertising

  3. google account

    Jeff Epler Guest

    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-----
     
    Jeff Epler, Jun 15, 2004
    #3
  4. (google account) wrote in message news:<>...
    <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!
     
    google account, Jun 15, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,858
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    974
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    553
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    669
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    724
Loading...

Share This Page