python 通过正则表达式模式解析DNS。例如“克[O] {2} GL [AEO] .COM”
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 通过正则表达式模式解析DNS。例如“克[O] {2} GL [AEO] .COM”相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python
import socket
import argparse
import sys
import re
def test_domain(domain_name):
try:
return bool(socket.gethostbyname(domain_name))
except socket.gaierror, e:
return False
RE_PARSE_EXPR = re.compile(r'\[([^\]]+)\](?:\{([0-9]+)\})?')
def gen_charset(expr_charset):
results = []
def genchars(char_range):
start, end = char_range.group(1, 2)
results.extend(range(ord(start), ord(end) +1))
expr_charset = re.sub(r'(.)\-(.)', genchars, expr_charset)
results.extend([ ord(i) for i in expr_charset ])
return results
class Generator(object):
def __init__(self, charsequences):
self.charsequences = charsequences
self.counts = [ len(q) for q in charsequences ]
self.total_combinations = reduce(lambda x, y: x * y, self.counts)
@property
def significance(self):
return reversed(list(enumerate(self.charsequences)))
def selectIndexSequence(self, value):
offset = 1
for i, q in self.significance:
index = ((value / offset) % len(q))
offset = offset * len(q)
yield q[index]
def getValueAtIndex(self, index):
return reversed(list(self.selectIndexSequence(index)))
@property
def maximum(self):
return 0 + self.total_combinations
@property
def all(self):
for i in range(0, self.maximum):
yield list(self.getValueAtIndex(i))
def buildFormatter(expr):
fields = []
def repl(match):
charset, howmany = match.group(1, 2)
if howmany is None:
howmany = 1
fields.extend([gen_charset(charset)] * (int(howmany)))
return ('%c') * int(howmany)
result = RE_PARSE_EXPR.sub(repl, expr)
return result, fields
def generateCombinations(values):
return Generator(values).all
def generateResults(format_str, values):
for combo in generateCombinations(values):
yield (str(format_str) % tuple(combo))
def main(args):
for expr_format in args.domain_format:
_format, _fields = buildFormatter(expr_format)
if not len(_fields):
if not test_domain(_format):
print "FAIL {0}".format(_format)
else:
for i in generateResults(_format, _fields):
if not test_domain(i):
print "FAIL {0}".format(i)
def options(args):
parser = argparse.ArgumentParser(description = 'domain resolver en-masse')
parser.add_argument('domain_format', metavar='FORMAT', type=str, nargs='+')
return parser.parse_args(args)
if __name__ == '__main__':
main(options(sys.argv[1:]))
以上是关于python 通过正则表达式模式解析DNS。例如“克[O] {2} GL [AEO] .COM”的主要内容,如果未能解决你的问题,请参考以下文章
剑指Offer-正则表达式匹配(Python)
匹配大量不同的句子(使用正则表达式模式解析)
python爬虫--解析网页几种方法之正则表达式
结合多个详细的python正则表达式
正则表达式 python 多行
正则表达式元字符扩展正则字符簇