亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python實(shí)現(xiàn)用戶管理系統(tǒng)

 更新時間:2018年01月10日 14:24:54   作者:諸葛來了也照燒  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)用戶管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python實(shí)現(xiàn)用戶管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

《python核心編程》第七章練習(xí)題第五題

一、題目描述

 userpw2.py。下面的問題和例題7.1中的管理名字-密碼的鍵值對數(shù)據(jù)程序有關(guān)。

(a) 修改那個腳本,使他能記錄用戶上次的登陸日期和時間(用time模塊),并與用戶密碼一起保存起來。程序的界面要求用戶輸入用戶名和密碼的提示。無論用戶名是否登陸成功,都應(yīng)有提示,在用戶登陸成功后,應(yīng)更新相應(yīng)用戶的上次登陸時間戳。如果本次登陸與上次登陸在時間上相差不超過四個小時,則通知該用戶:“You already in at :<last_login_timestamp>”。

(b) 添加一個管理菜單,其中有以下兩項:(1)刪除一個用戶 (2)顯示系統(tǒng)中所有用戶的名字和他們密碼的清單。

(c) 口令目前沒有加密。請?zhí)砑右欢螌诹罴用艿拇a

(d) 為程序添加圖形界面,例如,用Tkinter。(圖形化界面開發(fā)比較復(fù)雜,這里沒有用到。)

(e) 要求用戶名布區(qū)分大小寫。

(f ) 加強(qiáng)對用戶名的限制,不允許符號和空白符。

(g) 合并“新用戶”和“老用戶”兩個選項。如果一個新用戶試圖用一個不存在的用戶名登陸,詢問該用戶是否是新用戶,如果是肯定的,就創(chuàng)建該用戶。否則按老用戶的方式登陸。

二、程序中用到的模塊,解釋如下:

(1) re:正則表達(dá)式引擎,python中調(diào)用正則表達(dá)式的方法

(2) pickle:對象持久化,將數(shù)據(jù)寫入到磁盤中

(3) datetime:時間處理,用于記錄用戶登陸時間戳

(4) base64:base64加密模塊

(5) hashlib:hash加密模塊 

完整代碼:

#-*- coding:utf-8 -*- 
#2017.7.17
 
import re 
import pickle 
import base64,hashlib 
from datetime import datetime 
 
def Initialization(file_name): 
 '''''程序初始化,創(chuàng)建user.ini和time.ini文件''' 
 dict_test={'admin':'db69fc039dcbd2962cb4d28f5891aae1'} #創(chuàng)建超級管理員,默認(rèn)密碼為admin 
 f = file(file_name,'a+') #以追加的方式打開文件,避免文件被修改 
 if len(f.readlines()) ==0: #判斷程序是否為空,只在第一次運(yùn)行的時候初始化 
  if file_name=='user.ini': 
   pickle.dump(dict_test, f, True) 
  else: 
   pickle.dump({},f, True) 
 f.close() 
 
def encodepass(passwd): 
 '''''采用base64和md5雙層加密,破解可能幾乎為0''' 
 m = hashlib.md5() 
 pwd = base64.b64encode(passwd) 
 m.update(pwd) 
 return m.hexdigest() 
 
def time_order(user): 
 '''''記錄用戶登陸時間,結(jié)果保存在time.ini文件中''' 
 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()-time_value 
  try: 
   if t.hour<=4: 
    print 'You already logged in at:<last_login_timestamp>' 
  except: 
   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): 
 '''''用戶創(chuàng)建程序,由olduser調(diào)用''' 
 while True: 
  name = raw_input('Please input the username:') 
  if re.match(r'\w', name): #采用正則表達(dá)式檢測用戶名是否合法 
   pass 
  else: 
   print 'Username should be made of A~Z、a~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): 
 '''''用戶登陸程序''' 
 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): 
 '''''刪除一個用戶,但必須以管理員的身份''' 
 print 'Please login as admin' #管理員的身份才能刪除用戶 
 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): 
 '''''查看所有用戶,但必須以管理員的身份''' 
 print 'Please login as admin' #管理員的身份才能查看所有用戶 
 name = raw_input('Login:') 
 pwd = raw_input('passwd:') 
 passwd = db.get(name) 
 if passwd == encodepass(pwd) and name == 'admin': 
  for key in db: 
   print 'username: %10s ====> password: %10s' % (key,db[key]) 
 else: 
  print 'You can not check all users!' 
 
def resetuser(db): 
 '''''修改密碼,但必須正確的輸入老密碼''' 
 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(): 
 '''''程序運(yùn)行的主函數(shù)''' 
 fu = file('user.ini','r') 
 db = pickle.load(fu) 
 prompt = '''''(L)ogin Now
(Q)uit
(D)elet User
(C)heck All User
(R)eset Password
Enter choice:''' 
  
 done = False 
 while not done: 
  chosen = False 
  while not chosen: 
   try: 
    choice = raw_input(prompt).split()[0].lower() 
   except (EOFError,KeyboardInterrupt): 
    choice = 'q' 
   print '\nYou picked:[%s]' % choice 
   if choice not in 'lqdcr': 
    print '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__': 
 '''''系統(tǒng)有一個用戶名為admin 密碼為admin的超級用戶,請立即修改密碼!''' 
 print 'Welcome to User Information Management System!' 
 Initialization('user.ini') 
 Initialization('time.ini') 
 showmenu()

更多學(xué)習(xí)資料請關(guān)注專題《管理系統(tǒng)開發(fā)》。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • DRF跨域后端解決之django-cors-headers的使用

    DRF跨域后端解決之django-cors-headers的使用

    這篇文章主要介紹了DRF跨域后端解決之django-cors-headers的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • 詳解python 拆包可迭代數(shù)據(jù)如tuple, list

    詳解python 拆包可迭代數(shù)據(jù)如tuple, list

    拆包是指將一個結(jié)構(gòu)中的數(shù)據(jù)拆分為多個單獨(dú)變量中。下面通過本文給大家介紹python 拆包可迭代數(shù)據(jù)如tuple, list的相關(guān)資料,需要的朋友參考下吧
    2017-12-12
  • Python基礎(chǔ)總結(jié)之itertools模塊詳解

    Python基礎(chǔ)總結(jié)之itertools模塊詳解

    itertools模塊是Python中一個鮮為人知但功能強(qiáng)大的工具,它專注于高效、內(nèi)存友好的迭代器操作,使其成為處理大型或復(fù)雜數(shù)據(jù)集的理想選擇,今天我們一起探討Python標(biāo)準(zhǔn)庫中的一個隱藏的寶藏:itertools模塊,感興趣的朋友一起看看吧
    2024-06-06
  • Django Form 實(shí)時從數(shù)據(jù)庫中獲取數(shù)據(jù)的操作方法

    Django Form 實(shí)時從數(shù)據(jù)庫中獲取數(shù)據(jù)的操作方法

    這篇文章主要介紹了Django Form 實(shí)時從數(shù)據(jù)庫中獲取數(shù)據(jù)的相關(guān)知識,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-07-07
  • python使用Turtle庫繪制動態(tài)鐘表

    python使用Turtle庫繪制動態(tài)鐘表

    這篇文章主要為大家詳細(xì)介紹了python使用Turtle庫繪制動態(tài)鐘表,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • opencv實(shí)現(xiàn)車牌識別

    opencv實(shí)現(xiàn)車牌識別

    這篇文章主要為大家詳細(xì)介紹了opencv實(shí)現(xiàn)車牌識別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • python獲取元素在數(shù)組中索引號的方法

    python獲取元素在數(shù)組中索引號的方法

    這篇文章主要介紹了python獲取元素在數(shù)組中索引號的方法,實(shí)例分析了Python中index方法的相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • Python 3.x 連接數(shù)據(jù)庫示例(pymysql 方式)

    Python 3.x 連接數(shù)據(jù)庫示例(pymysql 方式)

    這篇文章主要介紹了Python 3.x 連接數(shù)據(jù)庫示例(pymysql 方式),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • Python中定時任務(wù)框架APScheduler的快速入門指南

    Python中定時任務(wù)框架APScheduler的快速入門指南

    APScheduler是基于Quartz的一個Python定時任務(wù)框架,實(shí)現(xiàn)了Quartz的所有功能,使用起來十分方便。下面這篇文章主要跟大家介紹了Python中定時任務(wù)框架APScheduler的快速入門指南,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-07-07
  • Python實(shí)現(xiàn)的根據(jù)IP地址計算子網(wǎng)掩碼位數(shù)功能示例

    Python實(shí)現(xiàn)的根據(jù)IP地址計算子網(wǎng)掩碼位數(shù)功能示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的根據(jù)IP地址計算子網(wǎng)掩碼位數(shù)功能,涉及Python數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下
    2018-05-05

最新評論