redis mysql验证 redis_mysql_check.py

2019/03/15 13:24
阅读数 4
# coding:utf-8

import pymysql
import redis
import sys


def con_mysql(sql):
    db = pymysql.connect(host='192.168.8.102',
                         user='root',
                         passwd='root',
                         port=3306,
                         db='test',
                         charset='utf8', )
    cursor = db.cursor()
    data = cursor.execute(sql)
    db.close()
    return data


def con_redis(name, passwd):
    pool = redis.ConnectionPool(host='192.168.8.102',
                                port=6379,
                                password='root', )
    r = redis.Redis(connection_pool=pool)
    r_passwd = r.hget(name, name)
    if not r_passwd:
        return False, '登录失败'
    if passwd == r_passwd.decode('utf8'):
        return True, '登录成功'
    else:
        return False, '登录失败'


def main(name, passwd):
    # name = sys.argv[1]
    # passwd = sys.argv[2]
    result = con_redis(name, passwd)
    if not result[0]:
        sql = '''select name from tb_login where name="%s" and passwd="%s"''' % (name, passwd)
        data = con_mysql(sql)
        if data:
            pool = redis.ConnectionPool(host='192.168.8.102',
                                        port=6379,
                                        password='root', )
            r = redis.Redis(connection_pool=pool)
            r.hset(name, name, passwd)
            # print('redis数据更新成功,账号密码正确!')
            return True
        else:
            # print('账号或者密码错误!')
            return False
    else:
        # print('登录成功!')
        return True


if __name__ == '__main__':
    main()

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部