码迷,mamicode.com
首页 > 编程语言 > 详细

python_tools

时间:2019-11-30 10:00:41      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:列表   失败   hex   decode   except   Nid   md5加密   更新   ssi   

tools

 1 import pymysql,hashlib,redis,time
 2 
 3 #操作数据库
 4 def op_mysql(sql,many=True):
 5     db_info = {user: xmb, password: 123456, host: 127.0.0.0, db: xmb, port: 3306,
 6                charset: utf8, autocommit: True}
 7     try:
 8         conn = pymysql.connect(**db_info)  # 建立连接
 9     except Exception as e:
10         print("mysql连接失败",e)
11         return "mysql连接失败"
12     cur = conn.cursor(pymysql.cursors.DictCursor)  # 游标
13     try:
14         cur.execute(sql)  # 执行sql语句
15     except Exception as e:
16         print("sql错误,%s"%sql)
17         result = "sql错误,%s"%sql
18     else:
19         if many:
20             result = cur.fetchall()     #fetchall返回的是列表
21         else:
22             result = cur.fetchone()  #fetchone返回的是字典
23     finally:
24         cur.close()
25         conn.close()
26     return result
27 op_mysql("select * from app_myuser")
28 
29 #md5加密
30 def md5(s,salt=‘‘):
31     new_s = str(s) + salt
32     m = hashlib.md5(new_s.encode())
33     return m.hexdigest()
34 
35 #操作redis
36 def my_redis(k,v=None,expire=60*60*2):
37     r = redis.Redis(host=127.0.0.0, password=HK139bc&*, port=6379, db=0, decode_responses=True)
38     if v:
39         r.set(k,v,expire)
40     else:
41         result = r.get(k)
42         return result
43 
44 #生成sessionid
45 def get_sessionid(username):
46     sessionid = %s%s%(username,time.time())
47     new_sessionid = md5(sessionid,‘‘)
48     return new_sessionid
49 
50 # def is_price(s):
51 #     s = str(s)
52 #     if s.isdigit():
53 #         return True
54 #     if s.count(‘.‘) == 1:
55 #         left,right = s.split(‘.‘)
56 #         # left = s.split(‘.‘)[0]
57 #         # right = s.split(‘.‘)[-1]
58 #         if left.isdigit() and right.isdigit():
59 #             return True
60 #     return False
61 
62 #判断价格
63 def is_price(s):
64     try:
65         price = float(s)
66     except Exception as e:
67         print("价格错误")
68         return False
69     return price
70 
71 #连接redis
72 def get_redis():
73     return redis.Redis(host=127.0.0.0, password=HK139bc&*, port=6379, db=0, decode_responses=True)
74 
75 #更新数据
76 def jiesuo():
77     sql = update app_myuser set error_count = 0 where username = "xmb";
78     result = op_mysql(sql)
79     print(result)

 

python_tools

标签:列表   失败   hex   decode   except   Nid   md5加密   更新   ssi   

原文地址:https://www.cnblogs.com/xumb/p/11961087.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!