def internal_error(e): if web['debug']: response = build_ret(False, msg=e.message, code=err_id) else: response = build_ret(False, msg="意外错误", code=err_id) logging.error("err_id:{}{}".format(err_id, traceback.format_exc())) return response
def internal_error(e): if web['debug']: response = err_ret(MSG) else: response = err_ret(MSG) logging.error("err_id:{}{}".format(err_id, traceback.format_exc())) return response
def sendmail(toaddrs, subject=email["subject"], msg=None): """ :param toaddrs: 收件人地址可多个["",""] :param subject: 邮件主题 :param msg: 邮件内容 :return: """ mail_msg = MIMEMultipart() if not isinstance(subject, unicode): subject = unicode(subject, 'utf-8') mail_msg['Subject'] = subject mail_msg['From'] = email["from_email"] mail_msg['To'] = ','.join(toaddrs) mail_msg.attach(MIMEText(msg, 'html', 'utf-8')) try: s = smtplib.SMTP_SSL() s.connect(email["smtp_addr"]) s.login(email["from_email"], email["from_email_pwd"]) s.sendmail(email["from_email"], toaddrs, mail_msg.as_string()) s.quit() logging.info("email send sucess. %s", ','.join(toaddrs)) except IOError: logging.error("Error unable to send email. %s", traceback.format_exc())
def send_sms_al(phone, param_string): url = 'https://sms.aliyuncs.com/' ts = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ') once = str(uuid.uuid4()) data = { 'Action': 'SingleSendSms', 'SignName': sign_name, 'TemplateCode': template_code, 'RecNum': phone, 'ParamString': param_string, 'Format': 'JSON', 'Version': '2016-09-27', 'AccessKeyId': access_key_id, 'SignatureMethod': 'HMAC-SHA1', 'Timestamp': ts, 'SignatureVersion': '1.0', 'SignatureNonce': once, } def __percent_encode(s): import urllib s = str(s) s = urllib.quote(s.decode('utf8').encode('utf8'), '') s = s.replace('+', '%20') s = s.replace('*', '%2A') s = s.replace('%7E', '~') return s def __gen_signature(data, req_method, secret): import hashlib import hmac import base64 sorted_data = sorted(data, key=lambda v: v[0]) vals = [] for k, v in sorted_data: vals.append(__percent_encode(k) + '=' + __percent_encode(v)) params = '&'.join(vals) string_to_sign = req_method + '&%2F&' + __percent_encode(params) key = secret + '&' signature = base64.encodestring( hmac.new(key, string_to_sign, hashlib.sha1).digest()).strip() return signature try: headers = { 'Content-Type': 'application/x-www-form-urlencoded', } signature = __gen_signature(data.items(), 'POST', access_key_secret) data['Signature'] = signature r = requests.post(url, data=data, headers=headers) resp = r.json() logging.info('fun_out: [sms_response: %2s]', json.dumps(resp)) print resp if resp.get('Model'): return True except Exception, e: logging.error('sms_err: [sms_err: %2s]', e)
def rollback(e): print 22333 g.session.rollback() response = build_ret(False, msg=e.message, code=err_id) logging.error("err_id:{}{}".format(err_id, traceback.format_exc())) return response
def bad_request(e): response = build_ret(False, msg="请求错误", code=err_id) logging.error("err_id:{}{}".format(err_id, traceback.format_exc())) return response
def json_validate_error(e): response = build_ret(False, msg=e.message, code=err_id) logging.error("err_id:{}{}".format(err_id, traceback.format_exc())) return response
def json_validate_error(e): MSG.msg = str(e) response = err_ret(MSG) logging.error("err_id:{}{}".format(err_id, traceback.format_exc())) return response