#1###############################################20170823用戶查詢認證python
[root@zabbix pickle_query]# more query.py ide
#!/usr/bin/python ui
#encoding=utf-8 ip
import tab,sys,pickle,getpass utf-8
f=open('/tmp/python/pickle_query/account.pkg','r') get
account=pickle.load(f) input
f.close() cmd
def query(): it
while True: class
name=raw_input('請輸入你的用戶名:')
if name == 'quit':
sys.exit()
if name in account:
while True:
password=getpass.getpass('請輸入你密碼:')
if password =='quit':
sys.exit()
#if password != passwd:
if password != account[name]:
print '對不起,密碼錯誤,請從新輸入密碼!'
continue
else:break
print '\033[36;1m歡迎登錄查詢\033[0m'
while True:
Flag=0
keyword=raw_input('請輸入你要查詢的人的姓名:')
if keyword == 'quit':
sys.exit()
if len(keyword)==0:
continue
f=open('/tmp/python/students.txt','r')
while True:
line=f.readline()
if len(line)==0:break
if len(line.strip())!=0:
ming = line.split()
namex = ming[1]
if keyword in namex:
print '\033[33;1m%s\033[0m'%line.rstrip()
Flag=1
else:continue
if Flag==0:
print '對不起,沒有你要查詢的信息,請從新輸入!'
f.close()
else:
print "對不起,用戶名輸入錯誤,請從新輸入!"
def main():
query()
if __name__=="__main__":
main()
[root@zabbix pickle_query]# more dict.py
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
#建立密碼文件,by xk
#time 20170825
import pickle
account={'user1':'passwd1','user1':'passwd2','user3':'passwd3','user4':'passwd4'}
f=open('/tmp/python/pickle_query/account.pkg','wb')
pickle.dump(account,f)
f.close()
#f=open('/tmp/python/pickle_query/account.pkg','r+')
#acc=pickle.load(f)
#acc['xukeqiang']='xkq'
#f.close()
#f=open('/tmp/python/pickle_query/account.pkg','r+')
#pickle.dump(acc,f)
#f.close()
#f=open('/tmp/python/pickle_query/account.pkg','r+')
#acc=pickle.load(f)
#f.close()
#print acc
[root@zabbix pickle_query]#
########################################################20170823用戶查詢認證,能夠正常退出
#!/usr/bin/python
#encoding=utf-8
while True:
input_name=raw_input('請輸入你的用戶名(退出請輸入quit):')
if input_name=='quit':break
name='xkq'
if input_name==name:
passwd='xkq'
while True:
input_passwd=raw_input('請輸入你的密碼(退出請輸入quit):')
if input_passwd=='quit':break
if input_passwd != passwd:
print '對不起,密碼錯誤,請從新輸入'
else:break
if input_passwd=='quit':break
print '歡迎登錄查詢'
while True:
Flag=0
f=open('/tmp/python/students.txt','r')
keyword=raw_input('請輸入你想要查詢的信息(退出請輸入quit):')
if keyword=='quit':
break
if len(keyword)==0:
f.close()
continue
keyword=keyword.strip()
if keyword=='':
f.close()
continue
while True:
line=f.readline()
if len(line)==0:break
if keyword in line:
print line,#加逗號,很少輸出一行空行
Flag=1
if Flag==0:
print '對不起,沒有查詢到你須要的信息,請從新輸入'
f.close()
else:print '對不起,用戶名錯誤,請從新輸入'
###################################################20170823
#將/tmp/python/students.txt中的oldword替換成newword
#!/usr/bin/python
#_*_ coding:utf-8 _*_
import fileinput
for line in fileinput.input('/tmp/python/students.txt',backup='.bak',inplace=1):
line=line.replace('oldword','newword')
print line,
############################################################備份21070826
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
import os,sys,time
#print sys.argv[-1]
#backup_file=sys.argv[1]
backup_time=time.strftime('%Y%m%d_%H%M%S',time.localtime())
#a=time.strftime('%Y/%m/%d_%H:%M:%S',time.localtime())
backup_cmd="tar -zcvf /tmp/python_%s.tar.gz %s"%(backup_time,sys.argv[1])
#backup_cmd="tar -zcvf /tmp/python_%s.tar.gz /tmp/python"%backup_time
os.system(backup_cmd)
###########################################################
~