Error smtplib in python -


i've python script, run on raspeberry pi monitoring server room , periodically send me e-mail , make other stuff. first time script running send notification , works fine, wait time (5 or 30 minutues) depending if alarm triggered, i've write loop wait. when loop finish, script make call of function sending e-mail , python return error. me ? test on debian 7.7.0 box , i've same error. below sample script , error. in advance. dom.

file "/usr/lib/python2.7/smtplib.py", line 361, in getreply raise smtpserverdisconnected("connection unexpectedly closed") smtplib.smtpserverdisconnected: connection unexpectedly closed  # import time import sleep import time import smtplib import sys import string smtp_server = "10.0.0.4" # mail server address mail_from = "server@domain.com"  mail_to = "user@gmail.com"  mail_subj = "server room notify"   server = smtplib.smtp(smtp_server) valore = 24 # value funtion  def sendalertemail(t_value):     log_t = time.strftime("%d/%m/%a %h:%m:%s ")     text = string.join("server room notify \n " + str(log_t) + str(t_value))     body_email = string.join((        "from: %s" % mail_from,        "to: %s" % mail_to,        "subject: %s" % mail_subj,        "",        text        ), "\r\n")     server.sendmail(mail_from,mail_to,body_email)     return  sendalertemail(valore) # calling here works fine in range(0,500):     sleep(1)     print(str(i))  sendalertemail(valore) # after 500 secs goes in error. test on debian 7.7.0 python 2.7.3 gcc 4.7.2 

smtplib opens tcp connection @ creation of smtp object. try reuse after 500 seconds of inactivity when timeout has closed connection.

you should keep server name, create new smtp object each connection in use case, because delay between sending messages high.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -