473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 2981
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David Hughes | last post by:
I wonder if anyone can help me resolve this problem. Although my ISP (One and One) provides the facility to run Python 2.2 CGI programs, their technical support people don't seem to have any Python...
2
by: Alex Hunsley | last post by:
I'm using the smtp module to send emails from python. I would like to specify several recipients of the email like follows: msg = MIMEText(time.ctime("Body text of email") me =...
21
by: Nancy | last post by:
Hi, Guys, Is there any other way to use python or mod_python writing a web page? I mean, not use "form.py/email", no SMTP server. <form action="form.py/email" method="POST"> ... Thanks a lot. ...
4
by: Philippe C. Martin | last post by:
Hi, I am testing the smtp module and have the following question: in the code below (taken from net sample) prior to adding the "Subject:" field, the email client found the "From" and the...
34
by: antonyliu2002 | last post by:
I've set up the virtual smtp server on my IIS 5.1 like so: 1. Assign IP address to "All Unassigned", and listen to port 25. 2. Access Connection granted to "127.0.0.1". 3. Relay only allow...
11
by: fdu.xiaojf | last post by:
Hi, I just want to send a very simple email from within python. I think the standard module of smtpd in python can do this, but I haven't found documents about how to use it after googleing....
1
by: William Connery | last post by:
Hi, I have a small python program with e-mail capabilities that I have pieced together from code snippets found on the internet. The program uses the smtplib module to successfully send an...
9
by: mmm | last post by:
After reading about and using the smtplib module, I thought code such as below would ignore the 'Cc: ' body line below when sending messages and instead simply use the RECEIVERS list session =...
6
by: phpmagesh | last post by:
Hi, I am using mail function in my php page, As soon i update the details, i have to send mail to the customer with the updated details, problem is when i send mail it simple through waring msg...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.