0
假設我有Python代碼是否有可能以編程方式傳遞給調用一個python實例(調用不同權限)
def my_great_func(an_arg):
a_file = open("/user/or/root/file", "w")
a_file.write("bla")
,我想保持不重視使用和不使用權限來invokation。同時我不想用sudo調用腳本/用sudo強制執行腳本(儘管這可能是一個小腳本)或者爲我的python
解釋器啓用setuid
(通常是一個壞主意......)。現在有一種想法是啓動python
解釋器的第二個實例並通過進程/管道進行通信。爲了最大限度地提高代碼的可維護性,只需將可調用對象傳遞給實例即可(例如,以subprocess.Popen
開頭並使用其PID),就像我將它傳遞給multiprocess.Process
(我無法使用,因爲我可以子進程中的t setuid
)。我想象像
# please consider this pseudo python code
pid = subprocess.Popen(["sudo", "python"]).get_pid()
thelib.pass_callable(pid, target, args)
甚至
interpreter_instance = greatlib.Python(target, args)
interpreter_instance.start()
interpreter_instance.wait()
這是可能的和現有庫覆蓋?