登陆加密小程序

自己想的注册登录小程序测试,使用md5加密存储用户填写的密码信息。登陆三次密码错误后退出

#!/usr/bin/env python
#_*_ coding:utf-8 _*_

import hashlib

def zhuce():

    username = raw_input("please input your username")
    password = raw_input('please input your password')
    hash = hashlib.md5()
    hash.update(password)
    with open('info.txt','r+') as fd:
        fd.write(username)
        fd.write('	')
        fd.write(hash.hexdigest())
        fd.write('
')
    print "注册成功"

def login(user,passwd):
    with open('info.txt') as fd:
        for line in fd:
            if user == line.strip('
').split()[0] and passwd == line.strip('
').split()[1]:
                return True
            else:
                return False

def menu():
    memu = '''
    1 注册
    2 登陆
    '''
    print memu

count = 0
retry = 3

while count < retry:
    menu()
    choice = raw_input('Please input your choice!')
    if choice == '1':
        zhuce()
    if choice == '2':
        user = raw_input('请输入用户名:')
        passwd = raw_input('请输入密码:')
        hash1 = hashlib.md5()
        hash1.update(passwd)
        password = hash1.hexdigest()
        result = login(user,password)
        if result:
           print 'success!'
            break
        else:
            print 'error'
            count += 1 
原文地址:https://www.cnblogs.com/dachenzi/p/6262536.html