我已经编写了一个应用程序,为用户提供了一个指定特定网络扫描工具(如nmap,xprobe,p0f)的选项,然后使用该工具扫描给定子网(我不重新实现该工具,只需在shell中调用它并解析它对我的应用程序的响应),解析结果并以特定格式存储在数据库中。此应用程序基本上是另一个应用程序的馈送应用程序,它将使用数据库中的数据。Python:获取导入模块的句柄
我写了我的代码,以便所有扫描工具界面都作为插件实现。该结构是
Project/
DBWriter.py
Scanner/
__init__.py
network_mapper.py
Scanner.py
Plugins/
__init__.py
nmap.py
p0f.py
Others/
所以要添加新的扫描界面,所有的开发者需要做的是写一个tool.py文件(在正确的语义ofcourse),并把它放到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
这将是一个出路,但我正在寻找PyInstaller提供的一个跨平台选项(几乎很漂亮) – RedBaron
好的。可能值得在你的问题中增加一个注释来说明你需要哪个平台。 –