intermittent smtp module problems (with sendmail) in python 2.2.1

Discussion in 'Python' started by Karl Ehr, Aug 4, 2004.

  1. Karl Ehr

    Karl Ehr Guest

    I have written the following simple program to monitor a single URL,
    and notify me via email whenever this URL changes. Intermittently,
    I raise the following exception:

    Traceback (most recent call last):
    File "./urlwatch.py", line 34, in ?
    server.sendmail(sourceAddress, emailAddress, message)
    File "/usr/lib/python2.2/smtplib.py", line 621, in sendmail
    if not (200 <= self.ehlo()[0] <= 299):
    File "/usr/lib/python2.2/smtplib.py", line 377, in ehlo
    (code,msg)=self.getreply()
    File "/usr/lib/python2.2/smtplib.py", line 328, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
    smtplib.SMTPServerDisconnected: Connection unexpectedly closed


    This is a copy of my code:

    ----------------------------------------
    #!/usr/bin/env python

    import os, sys, string, urllib, time, smtplib, shutil, filecmp

    ## functions & classes
    ## vars

    sleeptime = 900 # seconds to sleep between polls
    urltoget = 'http://some.domain.com/foo'
    server = smtplib.SMTP('localhost') # server to relay smtp through for
    alarms
    emailAddress = '' # address to send alerts to
    sourceAddress = '' # required! must have
    form
    message = 'Your monitored URL has changed!'

    ## Main:

    # establish baseline (old version)
    urlfile = urllib.urlretrieve(urltoget, 'oldurlfile')
    print "Sleeping", sleeptime, " seconds."
    time.sleep(sleeptime)

    while 1:
    try:
    urlfile = urllib.urlretrieve(urltoget, 'newurlfile')
    except:
    print "Could not retrieve url from internet"
    sys.exit(-1)

    if filecmp.cmp('oldurlfile','newurlfile',shallow=0):
    print "the url is still the same"
    else:
    print "the url has changed!!!"
    server.set_debuglevel(1)
    server.sendmail(sourceAddress, emailAddress, message)
    server.quit()
    time.sleep(10)

    shutil.copyfile('newurlfile','oldurlfile')

    # sleep until next iteration
    print "Sleeping", sleeptime, " seconds."
    time.sleep(sleeptime)

    #EOF

    ------------------------------------

    tia,

    Karl
     
    Karl Ehr, Aug 4, 2004
    #1
    1. Advertising

  2. Re: intermittent smtp module problems (with sendmail) in python2.2.1

    On 4 Aug 2004, Karl Ehr wrote:

    > I have written the following simple program to monitor a single URL,
    > and notify me via email whenever this URL changes. Intermittently,
    > I raise the following exception:
    >
    > smtplib.SMTPServerDisconnected: Connection unexpectedly closed


    This is probably happening because of a timeout on the SMTP server. Try
    moving this line:

    > server = smtplib.SMTP('localhost') # server to relay smtp through for


    To right in front of where you use it:

    > server.set_debuglevel(1)
    > server.sendmail(sourceAddress, emailAddress, message)
    > server.quit()


    the SMTP() constructor doesn't just set up a connection (and open it on
    ..sendmail()); it opens the connection when it's called and leaves it open
    until you call .quit() (or delete the object).
     
    Christopher T King, Aug 4, 2004
    #2
    1. Advertising

  3. Karl Ehr

    Karl Ehr Guest

    Christopher T King <> wrote in message news:<>...
    > On 4 Aug 2004, Karl Ehr wrote:
    >
    > > I have written the following simple program to monitor a single URL,
    > > and notify me via email whenever this URL changes. Intermittently,
    > > I raise the following exception:
    > >
    > > smtplib.SMTPServerDisconnected: Connection unexpectedly closed

    >
    > This is probably happening because of a timeout on the SMTP server. Try
    > moving this line:
    >
    > > server = smtplib.SMTP('localhost') # server to relay smtp through for

    >
    > To right in front of where you use it:
    >
    > > server.set_debuglevel(1)
    > > server.sendmail(sourceAddress, emailAddress, message)
    > > server.quit()

    >
    > the SMTP() constructor doesn't just set up a connection (and open it on
    > .sendmail()); it opens the connection when it's called and leaves it open
    > until you call .quit() (or delete the object).


    Aha!! Thank you this works fine now... I'll keep that snag in mind
    when referencing other "external" apps in the future...
     
    Karl Ehr, Aug 5, 2004
    #3
    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. Hmmm...
    Replies:
    4
    Views:
    2,139
    ChrisO
    Sep 6, 2004
  2. peterson
    Replies:
    0
    Views:
    724
    peterson
    Dec 30, 2003
  3. sugapablo
    Replies:
    0
    Views:
    2,187
    sugapablo
    Sep 21, 2007
  4. Andrzej Adam Filip
    Replies:
    0
    Views:
    4,147
    Andrzej Adam Filip
    Mar 31, 2008
  5. John W. Long

    smtp.sendmail security

    John W. Long, Jul 28, 2003, in forum: Ruby
    Replies:
    2
    Views:
    103
    Chris Morris
    Jul 29, 2003
Loading...

Share This Page