Updated code and seperated in functions
buildbot/travis_bionic Build done. Details

This commit is contained in:
kuldeep.k@cisinlabs.com 2022-02-24 20:59:12 +05:30
parent ac8fe8261e
commit f7d6fa0488
1 changed files with 25 additions and 21 deletions

46
main.py
View File

@ -30,6 +30,27 @@ class SMTPWebhookApp:
SMTP webhook server
"""
def create_new_client(self, response):
try:
print("- - - - - CREATING CLIENT - - - - -")
mythread.client = smtplib.SMTP(host=SMTP_SERVER_HOST,
port=SMTP_SERVER_PORT)
mythread.client.starttls()
except (smtplib.SMTPException, TimeoutError) as e:
soft_return("can't connect")
response = {"status": 500,
"message": "some error: {}".format(e)}
print("response1: ", response)
def smtp_login(self, response, msg):
try:
mythread.client.login(msg["From"], FROM_MAIL_PASSWORD)
except smtplib.SMTPException as e:
soft_return(e)
response = {"status": 500,
"message": "some error: {}".format(e)}
print("response2: ", response)
def _send_mail(self):
if not cherrypy.request.headers.get('Content-Length'):
@ -62,6 +83,7 @@ class SMTPWebhookApp:
msg['To'] = to_mail
c = 0
response = {}
while (c < 2):
try:
mythread.client.sendmail(msg['From'],
@ -71,24 +93,9 @@ class SMTPWebhookApp:
smtplib.SMTPServerDisconnected):
if (c == 1):
soft_return("can't connect")
try:
print("- - - - - CREATING CLIENT - - - - -")
mythread.client = smtplib.SMTP(host=SMTP_SERVER_HOST,
port=SMTP_SERVER_PORT)
mythread.client.starttls()
except (smtplib.SMTPException, TimeoutError) as e:
soft_return("can't connect")
response = {"status": 500,
"message": "some error: {}".format(e)}
print("response1: ", response)
try:
mythread.client.login(msg["From"], FROM_MAIL_PASSWORD)
except smtplib.SMTPException as e:
soft_return(e)
response = {"status": 500,
"message": "some error: {}".format(e)}
print("response2: ", response)
self.create_new_client(response)
self.smtp_login(response, msg)
except smtplib.SMTPException as e:
soft_return("can't send for some reason")
@ -132,10 +139,7 @@ if __name__ == "__main__":
cherrypy.tree.mount(ROOT, config={})
mythread = threading.local()
mythread.client = smtplib.SMTP(host=SMTP_SERVER_HOST,
port=SMTP_SERVER_PORT)
mythread.client.starttls()
mythread.client = None
if hasattr(ENGINE, "signal_handler"):
ENGINE.signal_handler.subscribe()
if hasattr(ENGINE, "console_control_handler"):