python_tools
Posted xmb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python_tools相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章