我已经设置了一个python脚本,用于在表格中插入新记录,发送电子邮件并在电子邮件成功发送后更新最近插入的记录。 Python脚本如下:
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,"**********************"
此脚本计划通过EC2实例上的Windows任务计划程序运行。这个脚本通常工作得很好,但这会偶尔发出一条错误信息。错误消息如下:
************************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 **********************
我试图复制此错误并失败。经过观察,我发现在失败时没有用户活动失败之间存在很高的相关性。在EC2计算机上没有最近的用户活动时,脚本仅会失败。有没有人遇到类似的东西?或任何建议/改进?
请原谅是否有任何缩进错误,可能是因为我将手动格式化,同时将脚本复制到StackOverflow的降价处。
答案 0 :(得分:0)
您的错误消息的上下文丢失了,但这似乎表明您尝试连接的远程方。
您很可能会在异常对象中找到更详细的信息。我建议连接调试器或记录异常的更多细节,比如堆栈跟踪以找到有问题的行。
也许traceback可能在这里不起作用。