老男孩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 =="1":
        tow_menu("HK")

        while chose_num !="q":
            chose_num=raw_input("please chose menu:")

            if chose_num=="1":
                three_menu("HK","fs")
            elif chose_num=="2":
                three_menu("HK","tz")
            elif chose_num=="b":
                tow_menu("HK")
                break
            elif chose_num=="q":
                exit()
            else:
                print("input error!")

    elif chose_num =="2":
        tow_menu("SH")
        while chose_num !="q":
            chose_num=raw_input("please chose menu:")

            if chose_num=="1":
                three_menu("SH","lz")

            elif chose_num=="b":
                tow_menu("SH")
                break
            elif chose_num=="q":
                exit()
            else:
                print("input error!")
    elif chose_num =="3":
        tow_menu("BJ")
        while chose_num !="q":
            chose_num=raw_input("please chose menu:")

            if chose_num=="1":
                three_menu("BJ","ft")
            elif chose_num=="2":
                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次則鎖定用戶登錄。

 1 #!/usr/bin/env python
 2 # -*-coding:UTF-8-*-
 3
4 5 6 def login(): 7 8 f=open("user",'r') #讀取user配置文件。 9 cont=f.readlines() #readlines把讀取的行當作元素返回一個列表 10 f.close() 11 allname=[] #初始化一個用戶列表 12 allpasswd=[] 13 for i in range(0,len(cont)-1): #len獲取cont列表的元素數量 14 onecont=cont[i].split() #循環取一行內容並分割成列表,split()以空格為分隔符分割字符串並返回一個列表。 15 onename=onecont[0] #循環取一行中的帳號 16 onepasswd=onecont[1] # 17 allname.append(onename) #循環把每一行取到的帳號追加到用戶列表中 18 allpasswd.append(onepasswd) 19 lf=open("user.lock",'r') 20 lcont=lf.readlines() 21 lf.close() 22 # print(lcont) #打印用戶鎖文件內容 23 # print(allname) 24 # print(allpasswd) 25 26 cont=0 27 while cont < 3: 28 username=raw_input("login user:").strip() 29 passwd=raw_input("password:") 30 if username not in allname: 31 print("No this accont!") 32 33 elif (username +"\n") in lcont: 34 print("your account has been locked!Please contact admin!") 35 break 36 else: 37 rel_passwd_index=allname.index(username) #取該帳號在用戶列表中的索引,此時用戶列表的索引和密碼列表的索引是對應的,因此我們同樣>取到了該帳號的密碼在密碼列表的索引 38 rel_passwd=allpasswd[rel_passwd_index] #取該帳號的真實密碼 39 if passwd==rel_passwd: 40 print("Login success!") 41 break 42 else: 43 print("password is error!") 44 cont+=1 45 if cont >= 3: 46 print("Excessive password error,your account has been locked!Please contact admin!") 47 nf=open("user.lock",'wb') 48 nf.write(username+"\n") 49 nf.close() 50 51 52 login()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM