久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Python基于gevent實(shí)現(xiàn)文件字符串查找器

瀏覽:9日期:2022-07-14 13:45:50

1、遞歸遍歷目錄下所有文件并通過finder函數(shù)定位指定格式字符串

2、用來查找字符串的finder函數(shù)是自己定義的,這里定義了一個(gè)ip_port_finder通過正則表達(dá)式查找ip:port格式(粗匹配:數(shù)字.數(shù)字.數(shù)字.數(shù)字:數(shù)字)的字符串

3、用gevent來實(shí)現(xiàn)協(xié)程并發(fā)完成耗時(shí)任務(wù)

代碼如下:

# -*- coding: utf-8 -*-import refrom os.path import joinfrom os import walkfrom gevent import monkeyimport geventmonkey.patch_all()def ip_port_finder(str: str) -> bool: pattern = re.compile(r'.+d+.d+.d+.d+:d+') matchObj = pattern.match(str) if matchObj: print('------') print(f'發(fā)現(xiàn)目標(biāo):{matchObj.group(0)}') return True else: return Falsedef find_in_file(file_path, finder): with open(file_path, 'r', encoding='utf-8', errors=’ignore’) as f: for (num, value) in enumerate(f): if finder(value):print(f'文件路徑:{file_path}')print(f'所在行數(shù):{num}')find_in_path_recursively = lambda path, finder: gevent.joinall( [gevent.spawn(find_in_file, join(root, file_name), finder) for root, directories, f_names in walk(path) for file_name in f_names])if __name__ == ’__main__’: path = 'E:dev_codesxxx' find_in_path_recursively(path, ip_port_finder)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 南华县| 平江县| 错那县| 德惠市| 凤庆县| 西乌| 安庆市| 金湖县| 佛坪县| 揭西县| 松桃| 沂水县| 房山区| 济阳县| 宜昌市| 榆社县| 西平县| 天镇县| 沭阳县| 民勤县| 温州市| 舞阳县| 南开区| 巨鹿县| 平陆县| 晋州市| 策勒县| 尚志市| 教育| 浮山县| 武功县| 中江县| 雅安市| 桦南县| 普洱| 上饶市| 丹东市| 封开县| 鄂州市| 米脂县| 富裕县|