Python:获取导入模块的句柄

发布于 2024-12-10 10:17:11 字数 1375 浏览 1 评论 0原文

我编写了一个应用程序,它为用户提供了一个选项来指定特定的网络扫描工具(例如 nmap、xprobe、p0f),然后使用该工具扫描给定的子网(我不重新实现该工具,只需在 shell 中调用它)并解析我的应用程序的响应),解析结果并以特定格式存储在数据库中。该应用程序基本上是另一个将使用数据库中的数据的应用程序的馈送应用程序。

我已经编写了代码,以便所有扫描工具接口都作为插件实现。结构是

Project/
   DBWriter.py 
   Scanner/
      __init__.py
      network_mapper.py
      Scanner.py
      Plugins/
         __init__.py
         nmap.py
         p0f.py
   Others/  

这样的,所以要添加一个新的扫描界面,开发人员需要做的就是编写一个 tool.py 文件(当然是正确的语义)并将其放入 Plugins 文件夹中。

为了实现这一点,我需要能够在运行时导入正确的 python 模块。

from Plugins import nmap
or
from Plugins import xprobe

根据用户输入的内容

这是我添加的代码

f,p,d = imp.find_module("Plugins")
x = imp.load_module("Plugins",f,p,d)
path_n = x.__path__
f,p,d = imp.find_module("%s"%(tool),path_n)
tool_mod = imp.load_module("%s"%(tool),f,p,d)
tool_mod.scan() #Irrespective of what user enters or what new plugin is  developed, this line does not need to change and will work as long as plug-in has a scan function

代码工作正常。但是,当我尝试使用 PyInstaller (适用于 Windows/*Nix 平台)将其捆绑到一个可执行文件中时,它有find_module 和 load_module 中遇到很多麻烦,因此我得到一个 ImportError (可能是因为路径在可执行解压缩中未正确展开)。

我的问题是 - Python 中是否有另一种方法可以实现相同的功能(希望可以与 PyInstaller 配合使用)?

我之前曾问过这个问题,在 PyInstaller 中寻找解决方法,但没有任何回应迫使我在 Python 中寻找解决方法

I have written an application that gives the user an option to specify a particular network scanning tool (say nmap,xprobe,p0f) and then scans a given subnet with that tool (I do not re-implement the tool, simply call it in shell and parse its response for my app), parses the result and stores in a particular format in DB. This application is basically a feeder application for another app that will use the data in DB.

I have written my code so that all Scan Tool interfaces are implemented as plug-ins. The structure is

Project/
   DBWriter.py 
   Scanner/
      __init__.py
      network_mapper.py
      Scanner.py
      Plugins/
         __init__.py
         nmap.py
         p0f.py
   Others/  

So to add a new scan interface, all the developer needs to do is write a tool.py file (in correct semantics ofcourse) and drop it into Plugins folder.

To achieve this I need to be able to import correct python module at run-time.

from Plugins import nmap
or
from Plugins import xprobe

Depending on what the user enters

Here's the code that I have added

f,p,d = imp.find_module("Plugins")
x = imp.load_module("Plugins",f,p,d)
path_n = x.__path__
f,p,d = imp.find_module("%s"%(tool),path_n)
tool_mod = imp.load_module("%s"%(tool),f,p,d)
tool_mod.scan() #Irrespective of what user enters or what new plugin is  developed, this line does not need to change and will work as long as plug-in has a scan function

The code works correctly. However, when I try to bundle it into one executable using PyInstaller (for Windows/*Nix platforms), it has a lot of trouble in find_module and load_module and consequently I get an ImportError (maybe its because the paths are not correctly unrolled in executable decompression).

My question is - Is there an alternate way in Python using which I can achieve the same functionality (that might hopefully work well with PyInstaller) ?

I had earlier asked this question looking for workarounds in PyInstaller, but no responses have forced me to look for workarounds in Python

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情痴 2024-12-17 10:17:11

如果您只需要在 Windows 上运行,那么我建议尝试 py2exe

py2exe 是一个 Python Distutils 扩展,它将 Python 脚本转换为可执行的 Windows 程序,无需安装 Python 即可运行。

我们在工作中使用它来加速一个非常大的Python程序,并且它可以处理导入的模块。

If you only need to run on Windows, then I recommend trying py2exe.

py2exe is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation.

We use it at work to speed up a very large Python program, and it copes with imported modules.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文