if __name__ == "__main__": parser = OptionParser( usage='%prog -i input -p path -t threads\r\nexample: %prog -i 1.txt -p "E:\\soft\\saomiao\\xray\\xray_windows_amd64.exe" -t 10') parser.add_option('-i', '--input', dest='input', help='要扫描的目标,txt格式,每行一个,如:1.txt') parser.add_option('-p', '--path', dest='path', help='xray的绝对路径,如:"D:\\Tools\\xray_windows_amd64.exe"') parser.add_option('-t', '--threads', dest='threads', help='线程数,默认10', default=10, type=int) options, args = parser.parse_args() if options.path and options.input: queue = get_targets(options.input) for i in range(options.threads): t = Thread(target=scan, args=(queue, options.path)) t.daemon = True t.start() queue.join()
时间: 2024-02-29 17:56:05 浏览: 136
这段代码中的主要作用是通过命令行参数调用扫描器程序,实现对目标文件列表中的每个目标进行漏洞扫描。具体实现过程如下:
1. 首先判断当前代码是否在主程序中执行,避免在被导入时执行该代码。
2. 使用 OptionParser 类创建一个命令行参数解析器 parser,并设置程序的使用说明和参数选项。
3. 添加三个参数选项:-i,-p,-t,分别表示要扫描的目标文件、xray 的绝对路径和线程数。其中,-i 和 -p 选项为必选项,-t 选项为可选项,默认值为 10。
4. 使用 parse_args() 方法解析命令行参数,将解析的结果存储在 options 和 args 两个变量中。
5. 判断是否同时指定了 -i 和 -p 选项,如果是,则调用 get_targets() 函数读取目标文件列表,将读取的目标添加到队列 queue 中。
6. 循环创建线程,每个线程都调用 scan() 函数进行漏洞扫描,传入的参数为目标队列 queue 和 xray 的绝对路径 options.path。设置线程为守护线程,启动线程。
7. 等待队列 queue 中的任务执行完毕,程序结束。
需要注意的是,这段代码依赖于 Python 内置的 OptionParser 类、Thread 类和 Queue 类,以及 get_targets() 和 scan() 两个函数。在使用该代码之前,需要先导入这些模块。
相关问题
if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--input_path', type=str, default='gh.xml') parser.add_argument('--output_path', type=str, default='./data') args_ = parser.parse_args() main(args_)
这段代码是 Python 中常用的命令行参数解析方法。其中,argparse 是 Python 自带的命令行参数解析库。在代码中,我们首先创建了一个 ArgumentParser 对象 parser,然后通过 parser.add_argument() 方法添加了两个参数,分别是 --input_path 和 --output_path,它们的默认值分别为 'gh.xml' 和 './data'。最后,我们使用 parser.parse_args() 方法解析命令行参数,并将其传递给 main 函数进行处理。
具体来说,if __name__ == '__main__': 这一行代码是 Python 中的惯用写法,表示当前脚本作为主程序运行。当我们通过命令行执行这个脚本时,会调用 main 函数,并将解析后的参数传递给它。
import logging import os.path import sys from optparse import OptionParser from gensim.corpora import WikiCorpus def parse_corpus(infile, outfile): '''parse the corpus of the infile into the outfile''' space = ' ' i = 0 with open(outfile, 'w', encoding='utf-8') as fout: wiki = WikiCorpus(infile, lemmatize=False, dictionary={}) # gensim中的维基百科处理类WikiCorpus for text in wiki.get_texts(): fout.write(space.join(text) + '\n') i += 1 if i % 10000 == 0: logger.info('Saved ' + str(i) + ' articles') if __name__ == '__main__': program = os.path.basename(sys.argv[0]) logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger(program) # logging.getLogger(logger_name) logger.info('running ' + program + ': parse the chinese corpus') # parse the parameters parser = OptionParser() parser.add_option('-i', '--input', dest='infile', default='zhwiki-latest-pages-articles.xml.bz2', help='input: Wiki corpus') parser.add_option('-o', '--output', dest='outfile', default='corpus.zhwiki.txt', help='output: Wiki corpus') (options, args) = parser.parse_args() infile = options.infile outfile = options.outfile try: parse_corpus(infile, outfile) logger.info('Finished Saved ' + str(i) + 'articles') except Exception as err: logger.info(err) # python parse_zhwiki_corpus.py -i zhwiki-latest-pages-articles.xml.bz2 -o corpus.zhwiki.txt 优化代码
import logging
import os.path
import sys
from optparse import OptionParser
from gensim.corpora import WikiCorpus
def parse_corpus(infile, outfile):
'''parse the corpus of the infile into the outfile'''
space = ' '
i = 0
with open(outfile, 'w', encoding='utf-8') as fout:
wiki = WikiCorpus(infile, lemmatize=False, dictionary={}) # gensim中的维基百科处理类WikiCorpus
for text in wiki.get_texts():
fout.write(space.join(text) + '\n')
i += 1
if i % 10000 == 0:
logging.info('Saved ' + str(i) + ' articles')
if __name__ == '__main__':
program = os.path.basename(sys.argv[0])
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(program) # logging.getLogger(logger_name)
logger.info('running ' + program + ': parse the chinese corpus')
# parse the parameters
parser = OptionParser()
parser.add_option('-i', '--input', dest='infile', default='zhwiki-latest-pages-articles.xml.bz2',
help='input: Wiki corpus')
parser.add_option('-o', '--output', dest='outfile', default='corpus.zhwiki.txt', help='output: Wiki corpus')
(options, args) = parser.parse_args()
infile = options.infile
outfile = options.outfile
try:
parse_corpus(infile, outfile)
logger.info('Finished Saved ' + str(i) + 'articles')
except Exception as err:
logger.info(err)
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![tgz](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)