# md5 加密
def my_md5(s,s):
new_s = str(s) + salt
m = hashlib.md5(new_s.encode())
return m.hexdigest()
# 发送邮件
def send_mail(subject,content,files=None):
smtp = yamail.SMTP(host=host,
user=user,
password=password,
)
smtp.send(to=to, cc=cc, subject=subject, contents=content, attachments=files)
# mysql 返回sql执行结果
mysql_info = {
"host": "118.24.3.XX",
"user": "XX",
"password": "123456",
"db": "jxz",
"port": 3306,
"charset": "utf8",
"autocommit": True
}
def execute_sql(sql, more=True, db_info=None):
# select * from user;
# select * from user where id = 1;
try:
if db_info:
conn = pymysql.connect(**db_info)
else:
conn = pymysql.connect(**mysql_info) # host=xxx,user=xx
except Exception:
traceback.print_exc()
return 3 # 数据库连接失败
cursor = conn.cursor(pymysql.cursors.DictCursor)
try:
cursor.execute(sql)
except:
print("sql语句不正确 %s" % sql)
return 4 # 代表sql不正确
else:
if more:
result = cursor.fetchall()
else:
result = cursor.fetchone()
finally:
cursor.close()
conn.close()
return result
# redis 连接对象
redis_info = {"host": "118.24.3.XX",
"password": "HK139bc&X",
"port": 6379,
"db": 0,
"decode_responses": True
}
def execute_redis():
r = redis.Redis(**redis_info)
return r
原文:https://www.cnblogs.com/zhangmeiyan/p/14379065.html