Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subprocess.Popen as different user on Windows

What is the best manner of launching a subprocess as a different user in Python on Windows? Preferably XP and up, but if it works only on Vista and 7, I can live with that too.

like image 737
Eric Pruitt Avatar asked Nov 25 '10 05:11

Eric Pruitt


People also ask

What is the difference between subprocess Popen and OS system?

os. system is equivalent to Unix system command, while subprocess was a helper module created to provide many of the facilities provided by the Popen commands with an easier and controllable interface. Those were designed similar to the Unix Popen command.

Do you need to close Popen?

Popen do we need to close the connection or subprocess automatically closes the connection? Usually, the examples in the official documentation are complete. There the connection is not closed. So you do not need to close most probably.


2 Answers

I am not sure if you can do this with the standard python libraries. However, the pywin32 package has a win32process.CreateProcessAsUser function which may be what you need.

like image 59
DaveP Avatar answered Sep 22 '22 21:09

DaveP


Another option is to popen not the desired process but runas ... command. Note that the Run As service should be enabled and running.

like image 45
khachik Avatar answered Sep 22 '22 21:09

khachik