Last active
August 29, 2015 14:25
-
-
Save amalgjose/640bb0e39dda8fcc6183 to your computer and use it in GitHub Desktop.
A simple python webservice using tornado. Email notification will be send for reach request.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__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