def send_html_email(receiver, title, content, attach_list=None): if not attach_list: attach_list = [] msg = MIMEMultipart() msg['From'] = sender msg['To'] = receiver msg['Subject'] = title # set html type paramater. msg.attach(MIMEText(content, 'html', 'utf-8')) for file_path in attach_list: with open(file_path, 'rb') as fp: attacf_f = MIMEImage(fp.read()) attacf_f['Content-Type'] = 'application/octet-stream' attacf_f['Content-Disposition'] = 'attachment;filename="%s"' % os.path.split(file_path)[1] msg.attach(attacf_f) try: smtp_obj = smtplib.SMTP() smtp_obj.connect(mail_host, 25) smtp_obj.login(mail_user, mail_pass) smtp_obj.quit() return True except smtplib.SMTPException as e: logging.error("Cannot send email: %s" % e) return False
def block_bad_request(ip): logging.debug("Analysis ip: %s" % ip) ip_info = get_cached_ip_info(ip) if ip_info is not None: logging.info("Got ip info from cache:%s -> %s, now don't proc it." % (ip, ip_info)) return True err_code, area = get_ip_info(ip) if err_code != 0: logging.error("In `block_bad_request` cannot get ip info: %s" % ip) return True from_china = bool( (u"\u5e02" in area or u"\u7701" in area or u"\u533a" in area) and (u"\u5730\u533a" not in area)) logging.debug("Got the ip(%15s) from %s, from china: %s." % (ip, area, from_china)) ip_info = {"from_china": from_china, "area": area} set_cached_ip_info(ip, info=ip_info) if from_china: return True # block black ip with open("/home/wwwroot/siteconf/black_ip_list.conf", "a+") as f: print >> f, "deny %s;" % ip command = "service nginx restart" os.system(command) return True
def block_bad_request(ip): logging.debug("Analysis ip: %s" % ip) ip_info = get_cached_ip_info(ip) if ip_info is not None: logging.info( "Got ip info from cache:%s -> %s, now don't proc it." % (ip, ip_info) ) return True err_code, area = get_ip_info(ip) if err_code != 0: logging.error("In `block_bad_request` cannot get ip info: %s" % ip) return True from_china = bool( (u"\u5e02" in area or u"\u7701" in area or u"\u533a" in area) and (u"\u5730\u533a" not in area) ) logging.debug("Got the ip(%15s) from %s, from china: %s." % (ip, area, from_china)) ip_info = {"from_china": from_china, "area": area} set_cached_ip_info(ip, info=ip_info) if from_china: return True # block black ip with open("/home/wwwroot/siteconf/black_ip_list.conf", "a+") as f: print >> f, "deny %s;" % ip command = "service nginx restart" os.system(command) return True
def send_html_email(receiver, title, content, attach_list=None): if not attach_list: attach_list = [] msg = MIMEMultipart() msg['From'] = sender msg['To'] = receiver msg['Subject'] = title # set html type paramater. msg.attach(MIMEText(content, 'html', 'utf-8')) for file_path in attach_list: with open(file_path, 'rb') as fp: attacf_f = MIMEImage(fp.read()) attacf_f['Content-Type'] = 'application/octet-stream' attacf_f[ 'Content-Disposition'] = 'attachment;filename="%s"' % os.path.split( file_path)[1] msg.attach(attacf_f) try: smtp_obj = smtplib.SMTP() smtp_obj.connect(mail_host, 25) smtp_obj.login(mail_user, mail_pass) smtp_obj.quit() return True except smtplib.SMTPException as e: logging.error("Cannot send email: %s" % e) return False
async def wrapper(request): try: response = await handler(request) except Exception as e: status_code = getattr(e, "status_code", 500) reason = getattr(e, "reason", "Internal Server Error") if status_code == 500: error_message = str(e) traceback_info = traceback.format_exc() logging.error("Error happend: %s\n%s\n" % (error_message, traceback_info)) content = "<center><h3>%s %s!</h3></center>" % (status_code, reason) response = HttpResponse(content, status=status_code, reason=reason) response.headers.add("Server", "madliar") return response
def send_text_email(receiver, title, content): msg = MIMEText(content, 'plain', 'utf-8') msg['Subject'] = title msg['From'] = sender msg['To'] = receiver try: smtp_obj = smtplib.SMTP() smtp_obj.connect(EMAIL_CONFIG.get("mail_host"), 25) smtp_obj.login(mail_user, mail_pass) smtp_obj.sendmail(sender, receiver, msg.as_string()) smtp_obj.quit() return True except smtplib.SMTPException as e: logging.error("Cannot send email: %s" % e) return False
def run_async_processor_server(): """Run a server to exec async task.""" obj = RedisMessageQueue() monitor = obj.subscribe() logging.info("[ASYNC]: Start the Async server.\n") while True: s = monitor.parse_response() try: start_proc_time = time.time() m_type, channel, msg = s msg = pickle.loads(msg) target_module = msg.get("target_module") target_name = msg.get("target_name") task_id = msg.get("id") trigger_time = msg.get("trigger_time") waitting_time = start_proc_time - float(trigger_time) logging.info( "[ASYNC]: Received an asynctask(%s) from [%s.%s], wait time: %.3f s." % (task_id, target_module, target_name, waitting_time) ) args = msg.get("args", ()) kwargs = msg.get("kwargs", {}) m = import_module(target_module) target = getattr(m, target_name) result = target(*args, **kwargs) cost_time = time.time() - start_proc_time logging.info( "[ASYNC]: Task(%s) from [%s.%s] exec finished, result: %s, cost: %.3f s." % (task_id, target_module, target_name, result, cost_time) ) except Exception as e: logging.error( "[ASYNC]: Task(msg: %s) exec failed: %s, \n%s" % (s, e, get_traceback()) )
def run_async_processor_server(): """Run a server to exec async task.""" obj = RedisMessageQueue() monitor = obj.subscribe() logging.info("[ASYNC]: Start the Async server.\n") while True: s = monitor.parse_response() try: start_proc_time = time.time() m_type, channel, msg = s msg = pickle.loads(msg) target_module = msg.get("target_module") target_name = msg.get("target_name") task_id = msg.get("id") trigger_time = msg.get("trigger_time") waitting_time = start_proc_time - float(trigger_time) logging.info( "[ASYNC]: Received an asynctask(%s) from [%s.%s], wait time: %.3f s." % (task_id, target_module, target_name, waitting_time)) args = msg.get("args", ()) kwargs = msg.get("kwargs", {}) m = import_module(target_module) target = getattr(m, target_name) result = target(*args, **kwargs) cost_time = time.time() - start_proc_time logging.info( "[ASYNC]: Task(%s) from [%s.%s] exec finished, result: %s, cost: %.3f s." % (task_id, target_module, target_name, result, cost_time)) except Exception as e: logging.error("[ASYNC]: Task(msg: %s) exec failed: %s, \n%s" % (s, e, get_traceback()))