首页 > 编程语言 > 详细

Python 工具包

时间:2021-02-05 19:14:23      阅读:23      评论:0      收藏:0      [点我收藏+]
# 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




Python 工具包

原文:https://www.cnblogs.com/zhangmeiyan/p/14379065.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!