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"