Beispiel #1
0
    def send(self):
        self.msg["Subject"] = self.title
        self.msg["From"] = self.sender
        self.msg["To"] = self.receiver

        #邮件正文
        if self.message:
            self.msg.attach(MIMEText(self.message))

        # 添加附件,支持多个附件(传入list),或者单个附件(传入str)
        if self.files:
            if isinstance(self.files, list):
                for f in self.files:
                    self._attach_file(f)

            elif isinstance(self.files, str):
                self._attach_file(self.files)

        #连接服务器并发送
        try:
            smtp_server = smtplib.SMTP(self.server)  #连接server
        except (gaierror and error) as e:
            logger.exception("发送邮件失败,无法连接到SMTP服务器,检查SMTP服务器及网络.%s", e)
        else:
            try:
                smtp_server.login(self.sender, self.password)  #登录
            except smtplib.SMTPAuthenticationError as e:
                logger.exception("用户名及密码验证失败--%s", e)
            else:
                smtp_server.sendmail(self.sender, self.receiver.split(";"),
                                     self.msg.as_string())  #发送邮件
            finally:
                smtp_server.quit()  #断开连接
                logger.info('发送邮件"{0}"成功! 收件人:{1}。如果没有收到邮件,请检查垃圾箱,'
                            '同时检查收件人地址是否正确'.format(self.title, self.receiver))
Beispiel #2
0
 async def clean() -> NoReturn:
     while True:
         try:
             await TempFile.clean()
         except Exception:
             logger.exception(
                 "Exception occurred during executing cleaning task:")
         await asyncio.sleep(3600)
Beispiel #3
0
 async def _refreshIdentity() -> NoReturn:
     while True:
         logger.info("Start trying to login pixiv account")
         try:
             await PixivAPIRoot.login()
         except Exception:
             logger.exception("Exception occurred during trying to login account:")
         await asyncio.sleep(
             PixivConstants.CONFIG["account"]["refresh-interval"].as_number()
         )
Beispiel #4
0
def encrypt(string, salt='', encrypt_way='MD5'):
    string += salt
    if encrypt_way.upper() == 'MD5':
        hash_string = hashlib.md5()
    elif encrypt_way.upper() == 'SHA1':
        hash_string = hashlib.sha1()
    else:
        logger.exception(EncryptError('请输入正确的加密方式,目前仅支持 MD5 或 SHA1'))
        return False

    hash_string.update(string.encode())
    return hash_string.hexdigest()
Beispiel #5
0
 def operation(self, sql, params=None):
     '''
     单条数据的操作,insert,update,delete
     :param sql:包含%s的sql字符串,当params=None的时候,不包含%s
     :param params:一个元祖,默认为None
     :return:如果执行过程没有crash,返回True,反之返回False
     '''
     try:
         # 执行SQL语句
         self.cur.execute(sql, params)
         # 正常结束事务
         self.conn.commit()
         return True
     except Exception as e:
         # 数据库回滚
         self.conn.rollback()
         logger.info("[sql_str_message]-%s" % cur.mogrify(sql, params))
         logger.exception(e)
         return False
Beispiel #6
0
    def send(self):
        self.msg['Subject'] = self.title
        self.msg['From'] = self.sender
        self.msg['To'] = self.receiver

        # 邮件正文
        if self.message:
            self.msg.attach(MIMEText(self.message, "html", 'utf-8'))

        # 添加附件,支持多个附件(传入list),或者单个附件(传入str)
        if self.files:
            if isinstance(self.files, list):
                for f in self.files:
                    self._attach_file(f)
            elif isinstance(self.files, str):
                self._attach_file(self.files)  # 将实例化中的path作为参数传递给实例化方法

        # 连接服务器并发送
        try:
            server = smtplib.SMTP(self.HOST, "25")  # 连接sever

        except (gaierror and error) as e:
            logger.exception('发送邮件失败,无法连接到SMTP服务器,检查网络以及SMTP服务器. %s', e)
        else:
            try:
                server.login(self.sender, self.password)  # 登录

            except smtplib.SMTPAuthenticationError as e:
                logger.exception('用户名密码验证失败!%s', str(e))
                pass
            else:
                server.sendmail(
                    self.sender,  # FROM
                    self.receiver.split(';'),  # TO
                    self.msg.as_string()  # message
                )  # 发送邮件
                logger.info('发送邮件"{0}"成功! 收件人:{1}。如果没有收到邮件,请检查垃圾箱,'
                            '同时检查收件人地址是否正确'.format(self.title, self.receiver))
                print('Email send success!')
        finally:
            server.quit()  # 断开连接