Python3用gevent写个文件字符串查找器
2024-09-27 21:57:18
[本文出自天外归云的博客园]
1、递归遍历目录下所有文件并通过finder函数定位指定格式字符串
2、用来查找字符串的finder函数是自己定义的,这里定义了一个ip_port_finder通过正则表达式查找ip:port格式(粗匹配:数字.数字.数字.数字:数字)的字符串
3、用gevent来实现协程并发完成耗时任务
代码如下:
# -*- coding: utf-8 -*-
import re
from os.path import join
from os import walk
from gevent import monkey
import gevent monkey.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 False def 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_codes\xxx"
find_in_path_recursively(path, ip_port_finder)
参考文章:
最新文章
- 使用代码为textview设置drawableLeft
- 深入解析Javascript中this关键字的使用
- 利用MetaWeblog API实现XMLRPC写博客功能
- characterCustomezition的资源打包代码分析
- Unique Binary Search Trees II
- Kernel-Scheduler implementation
- Object-C 对象 (创建/销毁 对象)-- 笔记
- Android学习总结——判断网络状态
- Qt写一个截屏工具(窗口透明)
- 二分图匹配之最佳匹配——KM算法
- mysql 查询今天,昨天,上个月sql语句 注解
- SharePoint 2010 电子书下载网站推荐
- volatile有什么用?能否用一句话描述volatile的应用场景
- mysql error(2003) 10060的再解决
- Python学习:经典编程例题
- 错误提示:The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object. Fix the build path then try building this project The type java.lang.Object cannot b
- 轻松制作X86 OPENWRT USB启动盘
- Python 中的变量
- win7下使用手动安装composer
- apache重写规则 rewrite