Skip to content

Instantly share code, notes, and snippets.

@amalgjose
Last active Aug 29, 2015
Embed
What would you like to do?
A simple python webservice using tornado. Email notification will be send for reach request.
__author__ = 'Amal G Jose'
import smtplib
class SendEmail(object):
def __init__(self):
self.email_id = "sender@gmail.com"
self.email_password = "password"
def send_email(self, job_id, job_status):
FROM = self.email_id
TO = ['recepient@email.com']
SUBJECT = "Mapreduce Job Status of Job ID : %s" %(job_id)
TEXT = "Status of JobId : %s was : %s" %(job_id, job_status)
# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(self.email_id, self.email_password)
server.sendmail(FROM, TO, message)
server.close()
print 'Email Sent Successfully'
except:
print "Failed to send email"
__author__ = 'Amal G Jose'
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from SendEmail import SendEmail
PORT = 8888
class JobStatus(tornado.web.RequestHandler):
def get(self):
job_id = self.get_argument("jobId", default="unknown", strip=True)
job_status = self.get_argument("status", default="unknown", strip=True)
notify = SendEmail()
notify.send_email(job_id, job_status)
self.write("Notified !!")
if __name__ == "__main__":
app = tornado.web.Application(handlers=[(r"/notification", JobStatus)])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(PORT)
print "Starting server"
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment