sofh7777 发表于 2018-8-15 07:26:13

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=a
  
    print 'new user is %s ' \
  
          'regiter time is %s' %(name,ul)
  
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==m.hexdigest():
  
      newtime = datetime.now()
  
      if (newtime-ul).days == 0 and (newtime-ul).seconds < 14400:
  
            print 'you already logged in at %s: ' %(ul)
  
      else:
  
            pwd=newtime
  
            print 'welcome back %s, login time is %s' %(name,passwd)
  
    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().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()
页: [1]
查看完整版本: python 简单的用户登陆