469,613 Members | 1,280 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

intermittent smtp module problems (with sendmail) in python 2.2.1

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 = 'u***@somedomain.com' # address to send alerts to
sourceAddress = 'f*******@somedomain.com' # required! must have
fo*@domain.com 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
Jul 18 '05 #1
2 2771
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).

Jul 18 '05 #2
Christopher T King <sq******@WPI.EDU> wrote in message news:<Pi**************************************@ccc 9.wpi.edu>...
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...
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by David Hughes | last post: by
2 posts views Thread by Alex Hunsley | last post: by
4 posts views Thread by Philippe C. Martin | last post: by
34 posts views Thread by antonyliu2002 | last post: by
9 posts views Thread by mmm | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.