Is there a way to execute commands as a specific user in Python?
As far as I know, there are about 3 ways to execute system commands in Python:
os.system(command) -> exit_status
os.popen(command [, mode='r' [, bufsize]]) -> pipe
Now I need to control the executors of the system commands, except like this:
os.system('su xxx; ' + command)
Is there a more elegant way to achieve the same effect?
All the things you mentioned (by the way, the
subprocess module has succeeded) is the way the process is generated. You sound like you’re looking for
setuids. You can call a function that will do this, such as
os.setuid, or, typically, depending on what your script does, you can run the entire script as an elevated user.