示例:用户登录

  • 业务过程如下:
  • 输入用户名、密码
  • 密码加密
  • 判断redis中是否记录了用户名,如果有则成功
  • 如果redis中没有用户名,则到mysql中查询
  • 从mysql中查询成功后,将用户名记录到redis中
#encoding=utf-8
from t2 import RedisHelper
from t3 import MysqlHelper
import hashlib

name=raw_input("请输入用户名:")
pwd=raw_input("请输入密码:")

sha1=hashlib.sha1()
sha1.update(pwd)
pwd1=sha1.hexdigest()

try:
    redis=RedisHelper()
    if redis.get('uname')==name:
        print 'ok'
    else:
        mysql=MysqlHelper('localhost',3306,'test1','root','mysql')
        upwd=mysql.get_one('select upwd from userinfos where uname=%s',[name])
        if upwd==None:
            print '用户名错误'
        elif upwd[0]==pwd1:
            redis.set('uname', name)
            print '登录成功'
        else:
            print "密码错误"
except Exception,e:
    print e.message

results matching ""

    No results matching ""