我有一个每周自动运行的python脚本。我希望脚本在遇到错误(例如来自锁定或丢失的文件)时发送电子邮件。出于明显的安全考虑,我不想使用需要登录的外部SMTP服务器
使用:Python3(虽然脚本将使用Python2.7.5运行,但使用3是因为我的Spyder配置为3,我更喜欢用它编写),Windows Server 2012
我相信我的问题是端口被阻塞,但我缺乏经验,无法真正理解各种nelnet命令的响应。我想验证我正在使用的python代码,以及我得到的错误,实际上是由于端口被阻塞而不是其他原因造成的
我使用管理员身份运行python的基本IDE中的代码。它返回:(“意外错误:”,)
我试过使用ports 25&;80,但我基本上是在端口上随机射击,因为我不知道(1)这是否是问题所在,或者(2)如何解释当我输入netstat时得到的CMD中的20+行-例如,an | find/I“25”
# Import modules
import sys, arcpy, datetime, traceback
import os
import shutil
# Import the email libraries
import smtplib, smtpd, asyncore, threading, ctypes
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
try:
if is_admin():
print("You an admin yay!")
# set datetime variable
d = datetime.datetime.now()
sdeTempPath = r"C:\tempUSIC"
if not os.path.exists(sdeTempPath):
os.mkdir(sdeTempPath)
print("Temporary directory not found. A new directory has been " + \
"created at {0}.".format(sdeTempPath))
else:
print("The temp USIC directory already exists at {0} and will be used.".format(sdeTempPath))
# open log file for holding errors
# log code skipped
##--------Set up SMTP Server Local
smtpServer = smtpd.SMTPServer(('localhost',80), None)
loop_thread = threading.Thread(target=asyncore.loop, name="Asyncore Loop")
loop_thread.start()
##Set the Email Server Information-------------------
# Set the server object that has the Outlook SMTP and port
fromAddr = 'robert.domiano@spireenergy.com'
toAddr = 'robert.domiano@spireenergy.com'
text = 'This is a test of sending email from within python.'
server = smtplib.SMTP('localhost',80)
server.ehlo()
server.starttls()
server.sendmail(fromAddr, toAddr, text)
server.quit()
else:
# This is the python 3 version
print("You not an admin boo.")
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
####main code stuff skipped here####
except:
#Rest of except code skipped
print("Unexpected error:", sys.exc_info()[0])
目前没有回答
相关问题 更多 >
编程相关推荐