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

python 简单的用户登陆

时间:2017-04-01 22:21:32      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:python

#!/usr/bin/env python
#coding:utf-8
import hashlib
from datetime import datetime
ul = {}
def newuser():
    a = []
    prompt = "Please enter username again:"
    while True:
        name = raw_input(prompt).lower()
        if not name.isalnum() and ‘‘ in name :
            print(‘name format error‘)
            continue
        else :
            if ul.has_key(name):
                prompt = "username already exists"
                continue
            else:
                break
    passwd = raw_input(‘enter password:‘)
    m=hashlib.md5()
    m.update(passwd)
    a.append(m.hexdigest())
    a.append(datetime.now())
    ul[name]=a
    print ‘new user is %s ‘           ‘regiter time is %s‘ %(name,ul[name][1])
def olduser():
    name= raw_input(‘Please enter username again:‘).lower()
    passwd = raw_input(‘Please enter your password:‘)
    m=hashlib.md5()
    m.update(passwd)
    pwd = ul.get(name)
    if pwd[0]==m.hexdigest():
        newtime = datetime.now()
        if (newtime-ul[name][1]).days == 0 and (newtime-ul[name][1]).seconds < 14400:
            print ‘you already logged in at %s: ‘ %(ul[name][1])
        else:
            pwd[1]=newtime
            print ‘welcome back %s, login time is %s‘ %(name,passwd[1])
    else:
        print ‘login incorrect‘
def removeuser():
    print ul
    name=raw_input(‘input a user name to remove: ‘).lower()
    if name in ul:
        ul.pop(name)
        print ‘remove successful‘
    else:
        print ‘this uesr not exist‘

def login():
    while True:
            name=raw_input(‘Please enter username:‘).lower()
            if not name.isalnum() and ‘‘ in name:
                print ‘name format error‘
                continue
            else:
                if not ul.has_key(name):
                    print ‘user name is not in userlist‘
                    answer=raw_input(‘register a new user? (y/n):‘).lower()
                    if ‘y‘==answer:
                        newuser()
                        break
                    elif ‘n‘==answer:
                        break
                else:
                    print ‘user name is already in db‘
                    olduser()
                    break

def showmenu():
    prompt = """
    (U)ser login
    (R)emove user
    (Q)uit
    Enter choice: """
    done = False
    while not done:
        chosen = False
        while not chosen:
            try:
                choice = raw_input(prompt).strip()[0].lower()
            except (EOFError,KeyboardInterrupt):
                choice= ‘q‘
            print"\nYou picked [%s]" %choice
            if choice not in ‘urq‘:
                print("invalid option,try again")
            else :
                chosen = True
        if choice == ‘q‘:done = True
        if choice == ‘u‘:login()
        if choice == ‘r‘:removeuser()

if __name__ == ‘__main__‘:
    showmenu()


python 简单的用户登陆

标签:python

原文地址:http://12393820.blog.51cto.com/12383820/1912375

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