def send_mail(title, msg): if not common.file_exist(common.CONST_DIR_LOG): common.create_directory(common.CONST_DIR_LOG) if not common.file_exist(common.CONST_DIR_CONF): common.create_directory(common.CONST_DIR_CONF) log = Logger(mail_log_filename, level='debug') config, err = _load_config() if config is None: log.logger.error(u"邮件发送客户端配置文件加载错误: %s", err) return host = config.get("host", "localhost") port = config.get("port", 25) user = config.get("user", "root") pwd = config.get("pwd", "") sender = config.get("sender", "localhost") receivers = config.get("receivers", []) message = MIMEText(msg, 'plain', 'utf-8') message['Subject'] = Header(title, 'utf-8') try: smtp_instance = smtplib.SMTP() smtp_instance.connect(host, port) # 25 为 SMTP 端口号 smtp_instance.login(user, pwd) smtp_instance.sendmail(sender, receivers, message.as_string()) log.logger.info(u"主题: [%s] 的邮件已经被发送." % title) except smtplib.SMTPException as err: log.logger.error(u"主题: [%s] 的邮件发送失败, 错误: %s" % (title, err.message))
def __init__(self): if not common.file_exist(common.CONST_DIR_LOG): common.create_directory(common.CONST_DIR_LOG) if not common.file_exist(common.CONST_DIR_CONF): common.create_directory(common.CONST_DIR_CONF) if not common.file_exist(common.CONST_DIR_DATABASE): common.create_directory(common.CONST_DIR_DATABASE) self.log = Logger(trader_log_filename, level='debug')
def _load_trader_config(): if not common.file_exist(trader_config_filename): return None, "config file: %s is not exist." try: with codecs.open(trader_config_filename, 'r', 'utf-8') as _file: return json.load(_file), None except Exception as err: return None, err.message
def _load_config(): if not common.file_exist(mail_config_filename): return None, u"配置文件: %s 不存在." try: with codecs.open(mail_config_filename, 'r', 'utf-8') as _file: return json.load(_file), None except Exception as err: return None, err.message
def _save_position_db_file(data): if common.file_exist(trader_db_position_filename): common.delete_file(trader_db_position_filename) try: common.dict_to_file(data, trader_db_position_filename) except Exception as err: return err.message return None
def _load_position_db_file(): if not common.file_exist(trader_db_position_filename): return None, "position file is not exist." try: position_data_set = common.file_to_dict( trader_db_position_filename) return position_data_set, None except Exception as err: return None, err.message
def _load_box_db_file(): if not common.file_exist(trader_db_box_filename): return None, "stock box file is not exist." try: box_data_set = common.file_to_dict(trader_db_box_filename) except Exception as err: return None, err.message box_timestamp = box_data_set.get("timestamp", None) box_value = box_data_set.get("value", None) if box_timestamp is None or box_value is None: return None, "stock box data error, data is null." current_timestamp = common.get_current_timestamp() if current_timestamp - box_timestamp > MAX_VALID_BOX_INTERVAL_HOURS: return None, "stock box data is too old." return box_value, None
def __init__(self): if not common.file_exist(common.CONST_DIR_LOG): common.create_directory(common.CONST_DIR_LOG) self.log = Logger(box_log_filename, level='debug') self.connect_instance = None
def _storage_box_data(data): if not common.file_exist(common.CONST_DIR_DATABASE): common.create_directory(common.CONST_DIR_DATABASE) common.dict_to_file(data, box_db_filename)