472,784 Members | 932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 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 2940
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.