Python automated O & M learning day 1-day1, python-day1

Source: Internet
Author: User

Python automated O & M learning day 1-day1, python-day1

I learned the homework I summarized on the first day of python automated O & M.

Knowledge used:
Json module for Data Conversion
Sys. exit is used to interrupt the loop exit program.
String formatting. format
Open and read dictionary and file
With open (file, "rw") as f:
F. redad/write
If: loop
Elif:
Else:
While: loop


Main purpose:
1. By default, the admin super-administrator user can achieve the following:
1). View blacklist users
2). Use admin to register a new user to the whitelist.
3). Remove the specified user from the blacklist
2. After the user enters the user name three times, the user will be locked for the last login.
3. the user name is entered correctly, but the password is entered incorrectly three times to automatically exit the program

4. Default whitelist User: hujianli password: admin #123
Huxiaojian password: admin #123
Xiaofei password: admin #123 enter the White List user to log on successfully. The welcome page is displayed. url information is provided.
5. Check the blacklist for each user input. An error is automatically reported in the blacklist. You need to unlock the blacklist.
6. automatically create three files under the same level Directory: whitelist directory \ blacklist directory \ admin hypermanaged directory === no database is replaced by a local file, and there is a database, information needs to be stored in the database
#-*-Coding: UTF-8-*-_ author _ = 'hujianlil' import json, OS, syssuper_admin = "to prevent users from remembering their passwords, A super administrator, admin, and password contact the Administrator to ask: "name =" Enter the user name for logon to the Web application after registration: "passwd =" enter the password corresponding to the logon User Name: "================================= common user logon section ================ ============== "wellcome_jm =" \ t congratulations on logging on to the system, username: {}, password {}. login successful. please remember the user name and password, \ n send you a link address: Network Disk update: http://pan.baidu.com/s/1sjsFrmX github update: https://github.com/liquanzhou/ops_doc """""" ==================== Creating a super-administrator user and a white list user ========== "admin_file =" admin. json "bmd_file =" baimingdan. json "Hmd_file =" heimingdan.txt "user_admin = {" admin ":" 123.com"} # create a hypervisor user name and password baimingdan = {"hujianli": "admin #123 ", "huxiaojian": "admin #123", "xiaofei": "admin #123"} # three normal logon users if not OS by default. path. isfile (admin_file): with open (admin_file, "wt") as f: json. dump (user_admin, f) if not OS. path. isfile (bmd_file): wi Th open (bmd_file, "wt") as f: json. dump (baimingdan, f) if not OS. path. isfile (Hmd_file): with open (Hmd_file, "wt") as f: f. write ("") # store the superuser user and password with open ("admin. json "," r ") as f: date = json. load (f) date_keys = "". join (date. keys () date_values = "". join (date. values () # userlist of users and passwords storing the whitelist = [] passwdlist = [] with open ("baimingdan. json "," r ") as f: date_bmi = json. load (f) # store the blacklist user name and password with open ("heimingd An.txt "," r ") as f: heimingdan = f. read (). strip ("\ n ") "================================== admin section ================ =========== "def admin_mb (): # cazuolist = [] quanxian = "(1) >>> view blacklist Users \ t input: 1 (2) >>> use admin to register a new user \ t input: 2 (3) >>> remove the specified user \ t input from the blacklist: 3 \ n "admin_jm =" welcome to the super Administrator Logon interface. The logon name is admin and the password is []. You can have the following permissions: \ n % s \ n "% (quanxian) print (admin_jm) caozuo = int (input (" select your operation: ") if caozuo = 1: print (heiming Dan) elif caozuo = 2: add_user = input (name) add_passwd = input (passwd) with open (bmd_file, "r") as f: add_info = json. load (f) if add_info [add_user] not in add_info.keys (): add_info [add_user] = add_passwd with open (bmd_file, "a") as f: json. dump (add_info, f) else: print ("this user already exists. Do not create % s again !!! "% Add_user) elif caozuo = 3: print. exit ("exit '''") else: "error =" passcount = 0 while count <3: count + = 1 username = input (name) with open (Hmd_file) as f: heimingdan = f. readlines () for lock_name in heimingdan: lock_name = lock_name.strip ("\ n") if username = lock_name: sys. exit ('====== WARNING: This account % s has been frozen ========' % username) if len (username) = 0: print ("\ 033 [1; 31m user name cannot be blank, error operation \ 033 [0 m") continue # process admin user if username = date_keys: I = 0 while I <3: I + = 1 passwd_of_user = input (passwd) if passwd_of_user = date_values: admin_mb () else: if I! = 3: print ('password % s is incorrect. Please think about the password, birthday or ID number, and % d chances '% (passwd_of_user, 3-I) else: sys. exit ("Sorry, Jianli reminder: Because your password is incorrect too many times, you can only exit the program ..!! ") # Process whitelist users for key, value in date_bmd.items (): if username = key: I = 0 while I <3: I + = 1 passwd_of_bmd_user = input (passwd) if passwd_of_bmd_user = value: print ("============================================ ========================================================== ======================== ") print ("============================================ ================================================ ======= ") print ("================================== ========================================================== ===================================== ") Print (wellcome_jm.format (username, passwd_of_bmd_user) break else: if I! = 3: print ('password % s is incorrect. Please think about the password, birthday or ID number, and % d chances '% (username, 3-I) else: sys. exit ("Sorry, Jianli reminder: Because your password is incorrect too many times, you can only exit the program ..!! ") Else: count! = 3 print ("user input error, % d chance" % (3-count) else: with open (Hmd_file, "a +") as f: f. write (username + "\ n") sys. exit ("User % s has reached the maximum number of logins. Please contact super administrator admin !! '"% Username) # dict_heimingdan ={}# for I in range (1): # name = input (BMD_user) # password = input (BMD_passwd) # if name not in dict_heimingdan.keys (): # dict_heimingdan [name] = password # with open ("heimingdan. json "," wt ") as f: # json. dump (dict_heimingdan, f)
View Code

 

 





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.