Python Simple User Login

Source: Internet
Author: User

#!/usr/bin/env python#coding:utf-8import hashlibfrom datetime import datetimeul  = {}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&nBsp;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: ')  &nbSP;&NBSP;&NBSP;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 ()                      breakdef 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 ':d one = true         if choice ==  ' u ': Login ()          if choice ==  ' R ': Removeuser () if __name__ ==  ' __main__ ':     showmenu ()


Python Simple User Login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.