国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Python基于gevent實現文件字符串查找器

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

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

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

3、用gevent來實現協程并發完成耗時任務

代碼如下:

# -*- 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'發現目標:{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'所在行數:{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)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 桑日县| 靖宇县| 开封县| 红安县| 葫芦岛市| 滨州市| 古田县| 确山县| 长丰县| 万山特区| 灯塔市| 明溪县| 浮梁县| 昆山市| 浏阳市| 襄汾县| 黄石市| 北宁市| 城口县| 神农架林区| 治县。| 莆田市| 南投县| 喀喇| 上杭县| 革吉县| 阿克| 东乡县| 剑阁县| 彝良县| 沙雅县| 杭州市| 泗阳县| 百色市| 从化市| 定兴县| 任丘市| 嘉峪关市| 博乐市| 鹿邑县| 深州市|