最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Python基于gevent如何实现文件字符串查找器
时间:2022-06-24 22:21:14 编辑:袖梨 来源:一聚教程网
Python基于gevent如何实现文件字符串查找器?这篇文章主要介绍了Python基于gevent实现文件字符串查找器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,下面小编就为大家介绍一下相关的内容,感兴趣的用户就来一聚教程网参考一下吧!
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_codesxxx"
find_in_path_recursively(path, ip_port_finder)
相关文章
- 《Pokemon TCG》Mega Evolution - Chaos Rising 系列 Crobat 全图卡牌情报公开 04-25
- TGA年度最佳《光与影 33号远征队》销量爆炸 官宣突破八百万 04-25
- 刺客信条 黑旗记忆重置版现价格异常,豪华版短时直降10美元引热议 04-25
- 全本小说账号怎么注销 04-25
- 异环新手实用开荒建议指南 04-25
- 怎么解锁《Kiln》中的所有成就 04-25