您的位置:首页 > 编程语言 > Python开发

用python来过滤无效用户名或密码

2014-01-13 13:00 316 查看
学了一段时间python,刚好工作中要用到一个除去无效用户名及密码的功能,现编了一段代码,主要实现以下功能:

1.去掉只有用户名,没有密码的行.

2.去掉密码长度小于6的行.

3.去掉针对一个用户名进行暴力破解,密码超过10次以上,认为是无效.

4.去掉用同一个密码暴力测试用户名超过10次以上,认为是无效.

上代码。

# -*- coding: cp936 -*-
import os,sys,time
os.chdir(sys.path[0])  #到当前目录

class countTxtFreq:
def __init__(self,infile,outfile,freq='user',fnum=10):
self.infile=infile
self.outfile=outfile
self.freq=freq
self.fnum=fnum
self.countFreq={}
self.Allrow=[]
self.Resultrows=[]
self._getFreq()
self._writeFile()

def _getFreq(self):
self.Allrow=[line.rstrip().split('\t',1) for line in open(self.infile) if line.find('\t')>1 and len(line.split('\t',1)[1])>7] #
if self.freq=='user':
for irow in self.Allrow:
if irow[0] in self.countFreq:
self.countFreq[irow[0]]+=1
else:
self.countFreq.setdefault(irow[0], 1)
#self.countFreq[irow[0]]=1
elif self.freq=='pass':                                            #
for irow in self.Allrow:
if irow[1] in self.countFreq:
self.countFreq[irow[1]]+=1
else:
self.countFreq.setdefault(irow[1], 1)
#self.countFreq[irow[1]]=1
else:
print u'请输入正确的提取参数!'

def _writeFile(self):         #
for i in self.Allrow:
if self.freq=='user':
if self.countFreq.get(i[0])<=self.fnum:
self.Resultrows.append(i[0]+'\t'+i[1]+'\n')
elif self.freq=='pass':
if self.countFreq.get(i[1])<=self.fnum:
self.Resultrows.append(i[0]+'\t'+i[1]+'\n')

out=open(self.outfile,'w')
out.writelines(self.Resultrows)
out.close()

if __name__ =='__main__':
time1=time.time()
a=countTxtFreq('pass_new.txt','pass_new2.txt','pass',2)
time2=time.time()
print u'一处处理了条%s记录,用时%s'%(str(len(a.Resultrows)),str(time2-time1))

'''
for key in a.countFreq:
print key,a.countFreq[key]
'''
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: