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

python核心编程--第其章 7.12 练习

时间:2014-06-05 05:29:08      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:python   核心编程   练习   


7.12 练习


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

# 7–5. userpw2.py. 下面的问题和例题7.1 中管理名字-密码的键值对数据的程序有关。
#    (a) 修改那个脚本,使它能记录用户上次的登录日期和时间(用time 模块),
#        并与用户密码一起保存起来。程序的界面有要求用户输入用户名和密码的提示。
#        无论户名是否成功登录,都应有提示,在户名成功登录后,应更新相应用户的上次
#        登录时间戳。如果本次登录与上次登录在时间上相差不超过4 个小时,则通知该用户:
#        “You already logged in at: <last_ login_timestamp>.”
#    (b) 添加一个“管理”菜单,其中有以下两项:(1)删除一个用户 (2)显示系统中所有用户
#        的名字和他们的密码的清单。
#    (c) 口令目前没有加密。请添加一段对口令加密的代码(请参考crypt, rotor, 或其它加密模块)
#    (d) 为程序添加图形界面,例如,用Tkinter 写。
#    (e) 要求用户名不区分大小写。
#    (f) 加强对用户名的限制,不允许符号和空白符。
#    (g) 合并“新用户”和“老用户”两个选项。如果一个新用户试图用一个不存在的用户名登录,
#        询问该用户是否是新用户,如果回答是肯定的,就创建该帐户。否则,按照老用户的方式登录。


# (a)
import time
import crypto
dbuserinfo = {}

def newuser():
    prompt = 'login desired: '
    while True:
        name = raw_input(prompt)
        if dbuserinfo.has_key(name):
            prompt = 'name taken, try another: '
            continue
        else:
            break
    pwd = raw_input('passwd: ')
    crypt_pwd = crypto.crypt(pwd)
    dbuserinfo[name] = []
    dbuserinfo[name].append(pwd)
    dbuserinfo[name].append(time.time())
    dbuserinfo[name].append(time.ctime())

def olduser():
    name = raw_input('login: ')
    pwd = raw_input('passwd: ')
    
    if dbuserinfo.has_key(name):
        passwd = dbuserinfo.get(name)[0]
        if passwd == pwd:
            print '*' * 10
            print 'welcome back', name
            print '*' * 10
            current_time = time.time()
            current_ctime = time.ctime()
            if (current_time - dbuserinfo[name][1]) < 60*60*4:
                print "You already logged in at: <%s>." % dbuserinfo[name][2]
            dbuserinfo[name][1] = current_time
            dbuserinfo[name][2] = current_ctime
        else:
            print '*' * 10
            print 'login incorrect'
            print '*' * 10
    else:
        print '*' * 10
        print 'login incorrect'
        print '*' * 10

# (b)

def deluser():
    prompt = 'delete user name: '
    delname = raw_input(prompt)
    if dbuserinfo.has_key(delname):
        del dbuserinfo[delname]
        print '*' * 10
        print "Delete success"
        print '*' * 10
    else:
        print '*' * 10
        print "User %s not exit." % delname
        print '*' * 10

def showusers():
    print '*' * 4, ' User list ', '*' * 4
    for user in dbuserinfo.keys():
        print user
        
def showmenu():
    prompt = """
Please choice:
 (N)ew User Login
 (E)xisting User Login
 (D)elete User
 (S)how All 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, IndexError) as e:
                choice = 'q'
            print '\nYou picked: [%s]' % choice
            if choice not in 'nedsq':
                print 'invalid option, try again'
            else:
                chosen = True

                if choice == 'q':
                    done = True
                elif choice == 'n':
                    newuser()
                elif choice == 'e':
                    olduser()
                elif choice == 'd':
                    deluser()
                elif choice == 's':
                    showusers()


showmenu()





python核心编程--第其章 7.12 练习,布布扣,bubuko.com

python核心编程--第其章 7.12 练习

标签:python   核心编程   练习   

原文地址:http://blog.csdn.net/ywj1225/article/details/27211639

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