Python 作业2

1.登陆系统
a)使它能记录用户上次的登录日期和时间(用 time 模块),并与用户密码一起保存起来。程序的界面有要求用户输入用户名和密码的提示。无论户名是否成功登录,都应有提示,在户名成功登录后,应更新相应用户的上次登录时间戳。如果本次登录与上次登录在时间上相差不超过4个小时,则通知该用户: “You already logged in at: last_login_time.”
(b) 添加一个“管理”菜单,其中有以下两项:(1)删除一个用户 (2)显示系统中所有用户的名字和他们的密码的清单
(c) 使用mysql完成

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

import MySQLdb
import time
import getpass

def Login():
    cur.execute("select * from userTable;")
    for i in range(1,4):
        username = raw_input("Please input new username:")
        password = getpass.getpass("Please input new password:")
        for u,p,t in cur.fetchall():
            if username == u and password == p:
                print "login in"
                logintime = time.time()
                if (int(t)-int(logintime)) > 14400:
                    print "You already logged in at:",time.ctime(t)
                upsql = "update userTable set time=%s where username=%s;"
                cur.execute(upsql,(logintime,username))
                conn.commit()
                return
        print "The username or password error"
        print "you have %d chances" %(3-i)
    return

def Create():
    username = raw_input("Please input new username:")
    cur.execute("select * from userTable;")
    for u,p,t in cur.fetchall():
        if username == u:
            print username,"is exist"
            return
    password = getpass.getpass("Please input new password:")
    sqlCre = 'insert into userTable(username,password,time) value(%s,%s,%s);'
    cur.execute(sqlCre ,(username,password,time.time()))
    conn.commit()

    print username,"successfully create"
    print

def Show():
    cur.execute("select * from userTable;")
    for u,p,t in cur.fetchall():
        print u
    print

def Delete():
    name = raw_input("please input you want delete username:")
    cur.execute("select * from userTable;")
    for u,p,t in cur.fetchall():
        if name == u:
            sqldel = "delete from userTable where username=%s;"
            cur.execute(sqldel,name)
            conn.commit()
            print name,"successfully delete "
            print
            return
    print name,"is not exist"
    print

def menu():
    print "MENU"
    print "Login in         (L)"
    print "Create new       (C)"
    print "Show anything    (S)"
    print "Delete           (D)"
    print "Exit             (E)"

def main():
    while 1:
        menu()
        select = raw_input("Please input your select :").upper()
        if select == "E":
            print "Good-bye"
            exit(0)
        elif select == "L":
            Login()
        elif select == "C":
            Create()
        elif select == "S":
            Show()
        elif select == "D":
            Delete()
        else:
            print "Input error,please enter again!!!"

conn = MySQLdb.connect(user="root",passwd="yang",db="westos")
cur = conn.cursor()

main()

cur.close()
conn.close()

2.备份文件

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

import time
import os

def backfile():
    files = ""
    while True:
        filename = raw_input("please input backfile name:")
        if filename != "end":
            if os.path.exists(filename):
                if " " in filename:
                    filename = filename.replace(" ","\ ")
                files = files + " " + filename
            else:
                print filename,"is not exist"
        else:
            return files

backfiles = backfile()
backtargz = "tar cf %s.tar %s &>/dev/null" %("aa"+time.strftime("%Y-%m-%d-%H-%M",time.localtime()),backfiles)
if os.system(backtargz) == 0 :
    print "back is successfully"
else:
    print "back is error"
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值