1 #coding=utf-8
2 import MySQLdb
3 import getpass
4 import time
5 import re
6
7
8
9
10 def db_insert(a,b,c=0):#新建用户
11 cur.execute(sqli,(a,b,c))
12 conn.commit()
13 def db_select(name=0,password=0,logintime=0):#查询信息
14 sqlselect="select * from userinfo "
15 show=cur.fetchmany(cur.execute(sqlselect))
16 for i in show:
17 if i[0] == name:
18 if password!=0:
19 return i[1]
20 if logintime!=0:
21 return i[2]
22 if password==0 and logintime==0:
23 return i[0]
24 conn.commit()
25 def db_updateinfo(name,newtime):#更新本次登陆时间
26 cur.execute(sqlupdate,(newtime,name))
27 conn.commit()
28 def deluserFunc():#删除用户
29 while 1:
30 print '#####username#####'
31 for i in show:
32 print '---',i[0]
33 username=raw_input("input username:")
34 if db_select(username) != None:
35 cur.execute(sqldel,(username))
36 conn.commit()
37
38 print "Done!"
39 while 1:
40 ex=raw_input("exit?(y/n)")
41 if ex == "y":
42 print "bye"
43 exit(0)
44 elif ex == "n":
45 menuFunc()
46 else:
47 print "input error"
48 else:
49 print "[username error!]"
50
51
52 def showuserinfoFunc():#显示用户信息
53 sqlselect="select * from userinfo "
54 show=cur.fetchmany(cur.execute(sqlselect))
55 for i in show:
56 print 'username:%-10s password:%s' %(i[0],i[1])
57 menuFunc()
58
59 def menuFunc():#显示菜单
60 print '''
61 #####MENU#####
62 (D)elete user
63 (S)how userinfo
64 (L)oginout
65 (E)xit
66 '''
67 choice=raw_input("input you choice:").upper()
68 while 1:
69 if choice == "D":
70 deluserFunc()
71 elif choice == "S":
72 showuserinfoFunc()
73 elif choice == "E":
74 print "bye"
75 cur.close()
76 conn.close()
77 exit(0)
78 elif choice=="L":
79 loginFunc()
80 else:
81 print "input error!"
82
83 def loginFunc():#用户登陆
84 j=0
85 i=3
86 while 1:
87 getusername=raw_input("input your username:")
88 m=re.search("\W+",getusername )
89 if m is None :
90 global getusername2
91 getusername2=getusername.lower()
92 if db_select(getusername2) != None :
93 while j < 3:
94 getpasswd=getpass.getpass("input your pass:")
95 if getpasswd == db_select(getusername2,1) :
96 loginTime=time.time()#本次登陆时间
97 lastLogintime1=db_select(getusername2,0,1)#读取上次登陆时间
98 lastLogintime2=time.ctime(float(lastLogintime1))#转换上次登陆时间格式
99 if float(loginTime)-float(lastLogintime1) < 14400:#若登陆时间相差不超过4小时
100 print "welcome,you are already logged in %s" %lastLogintime2
101 else:
102 print "welcome"
103 db_updateinfo(getusername2,loginTime)
104 menuFunc()
105 else:
106 i-=1
107 print "password error,you have %d chance" %i
108 j+=1
109 if j==3:
110 exit(0)
111 elif len(getusername2) >1 :
112 while 1:
113 adduser=raw_input("username unexist,do you want to add?(y/n)")#添加新用户
114 if adduser == "y":
115 while 1:
116 newpasswd=getpass.getpass("input newuser %s 's password:" %getusername)#输入密码
117 newpasswdagain=getpass.getpass("input password again:")
118 if newpasswd == newpasswdagain:
119 db_insert(getusername2,newpasswd)
120 print "useradd success"
121 loginFunc()
122 else:
123 print "error,password different!"
124 elif adduser == "n":
125 loginFunc()
126 else:
127 print 'input error!'
128 else:
129 print "[username lenth must > 1!]"
130 else:
131 print "[input error,username must make up of letters or numbers!]"
132
133 if __name__ == '__main__':
134
135 conn = MySQLdb.connect(user='root',passwd='',db='logininfo',host='localhost',charset='utf8')
136 cur = conn.cursor()
137 sqlselect="select * from userinfo "
138 show=cur.fetchmany(cur.execute(sqlselect))
139 sqli= "insert into userinfo(username,password,logintime) value(%s,%s,%s)"
140 sqlupdate= "update userinfo set logintime=%s where username=%s "
141 sqldel="delete from userinfo where username=%s"
142 loginFunc()