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