[issue44333] Segmentation fault

Kuldeep Daksh report at bugs.python.org
Mon Jun 7 05:50:39 EDT 2021


Kuldeep Daksh <mechatronickuldeep at gmail.com> added the comment:

I run same code on different window with python3.6. at other place smtp
working in a right way but as I run it in my project its still giving the
error. please find the attachment of my code.

On Mon, 7 Jun 2021 at 14:44, Kuldeep Daksh <report at bugs.python.org> wrote:

>
> Kuldeep Daksh <mechatronickuldeep at gmail.com> added the comment:
>
> I am having lunch right now.after lunch I will give you the information.
>
> On Mon, Jun 7, 2021, 14:42 Eric V. Smith <report at bugs.python.org> wrote:
>
> >
> > Eric V. Smith <eric at trueblade.com> added the comment:
> >
> > If you can't provide any more information, we can't help you and I'll
> have
> > to close this issue.
> >
> > ----------
> >
> > _______________________________________
> > Python tracker <report at bugs.python.org>
> > <https://bugs.python.org/issue44333>
> > _______________________________________
> >
>
> ----------
>
> _______________________________________
> Python tracker <report at bugs.python.org>
> <https://bugs.python.org/issue44333>
> _______________________________________
>

----------
Added file: https://bugs.python.org/file50095/alert_emailer.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44333>
_______________________________________
-------------- next part --------------
import os
import inspect
import logging
import smtplib,ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from jinja2 import FileSystemLoader, Environment

from alerts.models.alert_rule import AlertRule
from alerts.models.user import User
from alerts.models.asset import Asset
from alerts.repositories.user_alert_email_count_repo import UserAlertEmailCountRepository


class AlertEmailer:
    __instance = None
    __config = None
    __from = None
    __pwd = None
    __server = None
    __image_url = 'https://s3-us-west-2.amazonaws.com/s3-us-west-2.amazonaws.com.public-images/alert.png'
    __user_email_count_repo = None

    @staticmethod
    def getInstance(config):
        if AlertEmailer.__instance == None:
            AlertEmailer(config)
        return AlertEmailer.__instance

    def __init__(self, config):
        self.logger = logging.getLogger(__name__)
        if self.__config is None:
            self.__config = config
            self.context = ssl.create_default_context()
            with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=self.context) as server:

                self.__from = config.email_from
                self.__pwd = config.email_from_pwd
                server.login(self.__from, self.__pwd)
        if self.__user_email_count_repo is None:
            self.__user_email_count_repo = UserAlertEmailCountRepository(self.__config)
        if AlertEmailer.__instance is not None:
            raise Exception("This class is a singleton class!")
        else:
            AlertEmailer.__instance = self

    def send_email(self, title: str, al_msg: str, alert_rule: AlertRule):
        users = alert_rule.get_users()
        if len(users) > 0:
            recipients = []
            for user in users:
                if user.is_email_alerts_enabled():
                    self.logger.info(user.get_email())
                    if alert_rule.is_critical():
                        recipients.append(user.get_email())
                    elif self.__should_send_alert_email(user, alert_rule):
                        recipients.append(user.get_email())
            if len(recipients) > 0:
                self.__send_default(title, al_msg, recipients)
        else:
            self.logger.info("No recipient for sending email.")

    def send_fault_alert_email(self, title: str, al_msg: str, asset: Asset):
        users = ["ksingh at sensegrow.com"]
        if (len(users) > 0):
            recipients = ["ksingh at sensegrow.com"]
            # for user in users:
            # self.logger.info(user.get_email())
            # recipients.append(user.get_email())

            self.__send_default(title, al_msg, recipients)
        else:
            self.logger.info("No recipient for sending email.")

    def __should_send_alert_email(self, user: User, alert_rule: AlertRule):
        try:
            alert_count = self.__user_email_count_repo.get_alert_rule_count(user, alert_rule)
            alert_group_count = self.__user_email_count_repo.get_alert_rule_group_count(user, alert_rule)
            if user.get_alert_msg_rate_limit_throttle() > alert_count:
                if alert_group_count < 1:
                    self.__user_email_count_repo.update_count(user, alert_rule);
                    return True
            return False
        except Exception as ex:
            self.logger.error(ex)
            return False

    def __send_default(self, title: str, al_msg: str, recipients):
        current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
        file_name = 'alert_email_template.html'

        template_loader = FileSystemLoader(searchpath=current_dir)
        template_env = Environment(loader=template_loader)
        template = template_env.get_template(file_name)
        message = template.render(img_url=self.__image_url, heading=title, message=al_msg)
        body = MIMEText(message, 'html')
        msg = MIMEMultipart('alternative')
        msg['Subject'] = 'ioEYE Predict : ' + title
        msg['From'] = self.__from
        msg['To'] = ", ".join(recipients)
        msg.attach(body)

        self.__server.sendmail(self.__from, recipients, msg.as_string())
        self.logger.info("Email send successfully.")

    def send_test_email(self):
        recipients = ["ksingh at sensegrow.com", "ksingh+p3 at sensegrow.com"]
        print(", ".join(recipients))
        heading = 'RMS Velocity'
        al_msg = 'Motor-2 (sfrasdf) located at CP RMS Velocity axis H reading 9.85 mm/sec at 2020-02-13 13:03:52 PM has exceeded the threshold of 4.5 mm/sec .'
        current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
        file_name = 'alert_email_template.html'

        template_loader = FileSystemLoader(searchpath=current_dir)
        template_env = Environment(loader=template_loader)
        template = template_env.get_template(file_name)
        message = template.render(img_url=self.__image_url, heading=heading, message=al_msg)

        HTML_BODY = MIMEText(message, 'html')
        msg = MIMEMultipart('alternative')
        msg['Subject'] = 'ioEYE Predict : ' + 'High Acc Alert!'
        msg['From'] = self.__from
        msg['To'] = ", ".join(recipients)
        msg.attach(HTML_BODY)
        self.__server.sendmail(self.__from, recipients, msg.as_string())
        self.logger.info("Email send successfully.")


More information about the Python-bugs-list mailing list