我已經編寫了一個應用程序,爲用戶提供了一個指定特定網絡掃描工具(如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
好的。可能值得在你的問題中增加一個註釋來說明你需要哪個平臺。 –