老男孩python自动化运维作业1


#!/usr/bin/env pthon
#字典操作三级菜单 “b”返回上一级菜单,“q”退出。 menu={"BJ":{"cp":{1:1,2:2,3:3},
"ft":{1:4,2:5,3:6}},
"SH":{"lz":{1:1,2:2,3:3}
},
"HK":{"tz":{1:1,2:2,3:3},
"fs":{1:4,2:5,3:6}}}
def one_menu():
for i in range(0,len(menu.keys())):
print(str(i+1) +"."+ menu.keys()[i])
i+=1
one_menu() def tow_menu(city):
menu_list=menu.get(city).keys()
for i in range(0,len(menu_list)):
print (str(i+1) + "." + menu_list[i])
i+=1 def three_menu(city,area): area_menu=menu.get(city).get(area)
for i in range(0,len(area_menu)):
print(area_menu.items()[i]) chose_num="b"
while chose_num !="q":
chose_num=raw_input("please chose menu:") if chose_num =="":
tow_menu("HK") while chose_num !="q":
chose_num=raw_input("please chose menu:") if chose_num=="":
three_menu("HK","fs")
elif chose_num=="":
three_menu("HK","tz")
elif chose_num=="b":
tow_menu("HK")
break
elif chose_num=="q":
exit()
else:
print("input error!") elif chose_num =="":
tow_menu("SH")
while chose_num !="q":
chose_num=raw_input("please chose menu:") if chose_num=="":
three_menu("SH","lz") elif chose_num=="b":
tow_menu("SH")
break
elif chose_num=="q":
exit()
else:
print("input error!")
elif chose_num =="":
tow_menu("BJ")
while chose_num !="q":
chose_num=raw_input("please chose menu:") if chose_num=="":
three_menu("BJ","ft")
elif chose_num=="":
three_menu("BJ","cp")
elif chose_num=="b":
tow_menu("BJ")
break
elif chose_num=="q":
exit()
else:
print("input error!") elif chose_num =="b":
one_menu()
elif chose_num =="q":
exit()
else:
print("input error!")

#文件操作用户登录,提示用户名不存在 和 密码错误,密码错误超过3次则锁定用户登录。

 #!/usr/bin/env python
# -*-coding:UTF-8-*-
3
4 def login(): f=open("user",'r') #读取user配置文件。
cont=f.readlines() #readlines把读取的行当作元素返回一个列表
f.close()
allname=[] #初始化一个用户列表
allpasswd=[]
for i in range(0,len(cont)-1): #len获取cont列表的元素数量
onecont=cont[i].split() #循环取一行内容并分割成列表,split()以空格为分隔符分割字符串并返回一个列表。
onename=onecont[0] #循环取一行中的帐号
onepasswd=onecont[1] #
allname.append(onename) #循环把每一行取到的帐号追加到用户列表中
allpasswd.append(onepasswd)
lf=open("user.lock",'r')
lcont=lf.readlines()
lf.close()
# print(lcont) #打印用户锁文件内容
# print(allname)
# print(allpasswd) cont=0
while cont < 3:
username=raw_input("login user:").strip()
passwd=raw_input("password:")
if username not in allname:
print("No this accont!") elif (username +"\n") in lcont:
print("your account has been locked!Please contact admin!")
break
else:
rel_passwd_index=allname.index(username) #取该帐号在用户列表中的索引,此时用户列表的索引和密码列表的索引是对应的,因此我们同样>取到了该帐号的密码在密码列表的索引
rel_passwd=allpasswd[rel_passwd_index] #取该帐号的真实密码
if passwd==rel_passwd:
print("Login success!")
break
else:
print("password is error!")
cont+=1
if cont >= 3:
print("Excessive password error,your account has been locked!Please contact admin!")
nf=open("user.lock",'wb')
nf.write(username+"\n")
nf.close() login()
上一篇:原生javaScript完成Ajax请求


下一篇:安装J2EE的SDK报错:could not find the required version of the Java(TM)2 Runtime Environment in '(null)'的解决办法。