Skip to content

Instantly share code, notes, and snippets.

@lmatt-bit
Created January 5, 2015 02:22
Show Gist options
  • Select an option

  • Save lmatt-bit/2c62be504a24f347405a to your computer and use it in GitHub Desktop.

Select an option

Save lmatt-bit/2c62be504a24f347405a to your computer and use it in GitHub Desktop.
Send email with IronPython
# coding=utf-8
import System.Net.Mail
import httplib
import time
import datetime
import os.path
workingDir = 'E:\\IronPython-2.7.5'
logFile = os.path.join(workingDir,'check.log')
touchFile = os.path.join(workingDir,'touchFile')
def sendEmail(sender,to,title,message):
try:
client = System.Net.Mail.SmtpClient("smtpserver",25)
client.UseDefaultCredentials=True
client.Send(sender,to,title,message)
touch()
except Exception, e:
errorMessage = "sendEnail error:\n" + str(e)
print errorMessage
writeLog(errorMessage)
def sendServerDownMessage():
title = '[Warning]'
message = 'Server is down at ' + str(datetime.datetime.now())
sendEmail('[email protected]', '[email protected],[email protected]', title, message)
def testHttpConnection(host,port):
try:
connection = httplib.HTTPConnection(host,port)
connection.connect()
connection.close()
return True
except Exception, e:
return False
def writeLog(message):
with open(logFile, "a") as myfile:
myfile.write(message + '\n')
def touch():
f = open(touchFile, 'w')
f.write(str(time.time()))
f.close()
def handleSerpDown():
writeLog('Server is down: ' + str(datetime.datetime.now()))
if not os.path.exists(touchFile):
sendServerDownMessage()
if time.time() - os.path.getmtime(touchFile) > 60 * 10:
sendServerDownMessage()
else:
pass
def main():
while True:
try:
count = 3
ret = False
while count > 0:
ret = testHttpConnection("servername",80)
if ret:
break
else:
time.sleep(5)
count -=1
if not ret:
print 'Server is down', datetime.datetime.now()
handleSerpDown()
else:
print 'Server is alive', datetime.datetime.now()
time.sleep(3 * 60)
except Exception, e:
raise e
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment