Python:获取导入模块的句柄
我编写了一个应用程序,它为用户提供了一个选项来指定特定的网络扫描工具(例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只需要在 Windows 上运行,那么我建议尝试 py2exe。
我们在工作中使用它来加速一个非常大的Python程序,并且它可以处理导入的模块。
If you only need to run on Windows, then I recommend trying py2exe.
We use it at work to speed up a very large Python program, and it copes with imported modules.