Python implements the user management system, and python implements the Management System

Source: Internet
Author: User
Tags expression engine

Python implements the user management system, and python implements the Management System

The example in this article shares the code for implementing the user management system in python for your reference. The specific content is as follows:

Python core programming Chapter 7 exercise Question 5

I. Description

Userpw2.py. The following problems are related to the Data Program of the Management name-Password Key Value Pair in example 7.1.

(A) modify the script so that it can record the user's last login date and time (using the time module) and save it with the user password. The user is prompted to enter the user name and password. A prompt should be displayed regardless of whether the user name is successfully logged in. After the user logs in successfully, the last logon timestamp of the corresponding user should be updated. If the time difference between this login and the last login is no more than four hours, the user is notified: "You already in at: <last_login_timestamp> ".

(B) Add a management menu with the following two items: (1) delete a user (2) display the names and passwords of all users in the system.

(C) The password is not encrypted currently. Add a piece of password encryption code

(D) add a graphical interface for the program, for example, using Tkinter. (Graphical interface development is complicated and is not used here .)

(E) The user name must be case sensitive.

(F) Enhance user name restrictions. Symbols and blank spaces are not allowed.

(G) Merge the "new user" and "old user" options. If a new user attempts to log in with a non-existing user name and asks whether the user is a new user, if yes, create the user. Otherwise, log in as an old user.

2. The modules used in the program are explained as follows:

(1) re: Regular Expression Engine, which calls the regular expression method in python

(2) pickle: the object is persistent and data is written to the disk.

(3) datetime: Time Processing, used to record the user login Timestamp

(4) base64: base64 encryption module

(5) hashlib: hash encryption module

Complete code:

#-*-Coding: UTF-8-*-#2017.7.17 import re import pickle import base64, hashlib from datetime import datetime def Initialization (file_name): ''' program Initialization, create a user. ini and time. INI file '''dict_test = {'admin': 'db69fc039dcbd2962cb4d28f5891aae1 '} # create a super administrator. The default password is admin f = file (file_name, 'a + ') # Open the file in append mode to prevent the file from being modified if len (f. readlines () = 0: # determine whether the program is empty. initialize if file_name = 'user. ini ': pickle. dump (dict_test, f, Tr Ue) else: pickle. dump ({}, f, True) f. close () def encodepass (passwd): ''' uses base64 and md5 double-layer encryption. The cracking may be almost 0''' m = hashlib. md5 () pwd = base64.b64encode (passwd) m. update (pwd) return m. hexdigest () def time_order (user): ''' records the user logon time, and the results are saved in time. ''' ft = file ('time. ini ', 'R') dbt = pickle. load (ft) if user not in dbt: dbt. setdefault (user, datetime. today () else: time_value = dbt [user] t = datetime. today ()-ti Me_value try: if t. hour <= 4: print 'You already logged in at: <last_login_timestamp> 'timeout T: print 'You already logged in at: <last_login_timestamp> 'dbt [user] = datetime. today () ft = file ('time. ini ', 'w') pickle. dump (dbt, ft, True) ft. close () def newuser (db): ''' the user creates a program and the olduser calls '''while True: name = raw_input ('Please input the username :') if re. match (R' \ W', name): # use a regular expression to check whether the user name is valid. pass else: pr Int 'username shoshould be made of ~ Z, ~ Z, 0 ~ 9. _ 'continue for valuename in db. keys (): if name. lower () = valuename. lower (): break else: break passwd = raw_input ('Please input the password: ') db [name] = encodepass (passwd) def olduser (db ): ''' user Login program ''' name = raw_input ('login: ') if name in db: pwd = raw_input ('passwd:') passwd = db. get (name) if passwd = encodepass (pwd): print 'Welcome back! ', Name time_order (name) else: print 'login incorrent! 'Else: YN = raw_input ('Do you want to instead a new user? Yes or No: ') if YN. lower () = 'yes': newuser (db) print '\ n', def deluser (db): ''' delete a user, however, you must use the Administrator identity '''print 'Please login as admin' # as the Administrator to delete the user name = raw_input ('login: ') pwd = raw_input ('passwd: ') passwd = db. get (name) if passwd = encodepass (pwd) and name = 'admin': user = raw_input ('Please input a user name: ') if user! = 'Admin': if db. pop (user): print 'delete Current! 'Else: print 'Con not delete admin! 'Else: print 'wrong passwprd' def checkuser (db): ''' to view all users, however, you must use the Administrator identity '''print 'Please login as admin' # as the Administrator to view all user names = raw_input ('login: ') pwd = raw_input ('passwd: ') passwd = db. get (name) if passwd = encodepass (pwd) and name = 'admin': for key in db: print 'username: % 10 s ===> password: % 10s' % (key, db [key]) else: print 'You can not check all users! 'Def resetuser (db): ''' to change the password, but you must enter the correct old password ''' name = raw_input ('Please input the username :') passwd = raw_input ('Please input old password: ') if db [name] = encodepass (passwd): passwd = raw_input ('Please input new password :') db [name] = encodepass (passwd) else: print 'wrong password! 'Def showmenu (): ''' main function for running the program ''' fu = file ('user. ini ', 'R') db = pickle. load (fu) prompt = ''' (L) ogin Now (Q) uit (D) elet User (C) heck All User (R) eset PasswordEnter choice: '''done = False while not done: chosen = False while not chosen: try: choice = raw_input (prompt ). split () [0]. lower () Partition T (EOFError, KeyboardInterrupt): choice = 'q' print '\ nYou picked: [% s]' % choice if choice not in 'lqdcr ': pri Nt 'invalid option, try again 'else: chosen = True if choice = 'q': done = True if choice = 'l': olduser (db) if choice = 'D': deluser (db) if choice = 'C': checkuser (db) if choice = 'r': resetuser (db) fu = file ('user. ini ', 'w') pickle. dump (db, fu, True) fu. close () if _ name _ = '_ main _': ''' the system has a super user whose username is admin and whose password is admin, please change your password now! '''Print' Welcome to User Information Management System! 'Initialization ('user. ini ') Initialization ('time. ini') showmenu ()

For more information, see management system development.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.