且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python脚本

更新时间:2022-10-04 22:11:06

#1###############################################20170823用户查询认证

[root@zabbix pickle_query]# more query.py                                                

#!/usr/bin/python                                                                        

#encoding=utf-8                                                                          

import tab,sys,pickle,getpass                                                            

f=open('/tmp/python/pickle_query/account.pkg','r')                                       

account=pickle.load(f)                                                                   

f.close()                                                                                

def query():                                                                             

  while True:                                                                            

        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)

###########################################################

~                                                     
















本文转自shangshanyang51CTO博客,原文链接:http://blog.51cto.com/qqran/1958632 ,如需转载请自行联系原作者