0

I have set up a python script to insert a new record into a table, send out an email and update the most recently inserted record once the email is sent out successfully. The Python Script is as follows:

import smtplib
import pyodbc
import time
import os
import time
from smtplib import SMTPException
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
localtime = time.asctime( time.localtime(time.time()) )
fromAddress = "sender@mydomain.com"
toAddress = "myEmail@mydomain.com"
email = MIMEMultipart()
email['From'] = fromAddress
email['To'] = toAddress
connection = pyodbc.connect('DRIVER={SQL Server};SERVER=mySQLServer;DATABASE=mydb;UID=username;PWD=password')
cursor = connection.cursor()
query = "INSERT INTO [mydb].[dbo].[Logger] ([ScriptStartTime], [EmailSent?]) VALUES (CURRENT_TIMESTAMP, 0);"
cursor.execute(query)
print "************************Python Script Log Started at", localtime,"**********************"
def update():
   updateEmailFlagQuery = ("UPDATE [mydb].[dbo].[Logger] SET [EmailSent?] = 1, [EmailSentTime] = CURRENT_TIMESTAMP WHERE ID = (SELECT MAX(ID) FROM [mydb].[dbo].[Logger])")
   updateEmailFlag = cursor.execute(updateEmailFlagQuery)
   connection.commit()
def notify(email, body, fromAddress, toAddress):
   email['Subject'] = "It Worked"
   email.attach(MIMEText(body, 'plain'))
   server = smtplib.SMTP('SMTPServer', 25)
   server.set_debuglevel(True)
   text = email.as_string()
   server.sendmail(fromAddress, toAddress, text)
   server.quit()
   connection.commit()
try:
   body = "This is an Email sent from Scheduled Python Script"
   notify(email, body, fromAddress, toAddress)
   print "Email Sent successfully"
   try:
      update()
      print "Update Statement on Success code-block executed successfully
   except:
      connection.rollback()
      print "Update Statement failed and update Transaction Rolled back"
except SMTPException, e:
   print e+" SMTP ERROR OCCURED"
except Exception, e:
   print e
   print "Unexpected Error/Exception";
print "************************Python Script Log Ended at", localtime,"**********************"

This script is scheduled to run via Windows Task Scheduler on an EC2 Instance. This script works just fine usually but this spits out an error message one in a while. The Error Message is as follows:

************************Python Script Log Started at Thu Jun 07 03:40:04 2018 **********************
[Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Unexpected Error/Exception
************************Python Script Log Started at Thu Jun 07 03:40:04 2018 **********************

I've tried to replicate this error and failed. Upon observation, I found a high co-relation between failure to no user activity around the time of failure. The Script only fails during the times there's no recent user activity on the EC2 machine. Did anyone come across something similar? or any suggestions/improvements?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Prem27
  • 1
  • 1

1 Answers1

0

The context of your error message is missing, but this seems to indicate that the remote party you are trying to connect to.

You most likely will find more detailed information in your exception object. I would suggest to connect a debugger or log more details of your exception, like the stack trace to find the offending line.

Perhaps traceback might be off help here.

Jonathan
  • 748
  • 3
  • 20