intermittent smtp module problems (with sendmail) in python 2.2.1

K

Karl Ehr

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 = '(e-mail address removed)' # address to send alerts to
sourceAddress = '(e-mail address removed)' # required! must have
(e-mail address removed) 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
 
C

Christopher T King

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).
 
K

Karl Ehr

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


To right in front of where you use it:


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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top