[本文出自天外归云的博客园]

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)

参考文章:

Python多任务实现之协程并发下载多图片

最新文章

  1. 使用代码为textview设置drawableLeft
  2. 深入解析Javascript中this关键字的使用
  3. 利用MetaWeblog API实现XMLRPC写博客功能
  4. characterCustomezition的资源打包代码分析
  5. Unique Binary Search Trees II
  6. Kernel-Scheduler implementation
  7. Object-C 对象 (创建/销毁 对象)-- 笔记
  8. Android学习总结——判断网络状态
  9. Qt写一个截屏工具(窗口透明)
  10. 二分图匹配之最佳匹配——KM算法
  11. mysql 查询今天,昨天,上个月sql语句 注解
  12. SharePoint 2010 电子书下载网站推荐
  13. volatile有什么用?能否用一句话描述volatile的应用场景
  14. mysql error(2003) 10060的再解决
  15. Python学习:经典编程例题
  16. 错误提示: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
  17. 轻松制作X86 OPENWRT USB启动盘
  18. Python 中的变量
  19. win7下使用手动安装composer
  20. apache重写规则 rewrite

热门文章

  1. 《Android进阶之光》--Material Design
  2. python tkinter-窗体
  3. js算法初窥06(算法模式03-函数式编程)
  4. vue 之 加载 iframe 的处理
  5. 13,EasyNetQ-错误条件
  6. CKEditor 4.4.1 添加代码高亮显示插件功能--使用官方推荐Code Snippet插件
  7. navicat 官方使用手册,中文版,快捷键大全
  8. struts2标签在jsp页面中构建map集合,循环显示
  9. android 6.0 动态权限
  10. winform自动更新之AutoUpdater.NET