简体   繁体   中英

Python starting subprocess as admin without calling process being admin

I am trying to write a program in python that consists of several parts:

  1. a config utility
  2. a hardware monitor
  3. a background process

The idea being that once installed (using cx_freeze) the hardware monitor is constantly running in the background, when a piece of compatible hardware (using d2xx driver for FTDI devices) is connected it checks the registry to see if it has been previously configured, if it has then it starts the background process with the serial number as an argument, however if not it starts the config utility.

However the hardware monitor needs to be running from start-up and as it only reads from the registry doesn't need full admin privileges, and the background process only reads so also does not need admin provileges, but the config utility needs to be able to write to the registry and hence needs admin.

My question is this: How can I call another program from within python as admin and with arguments?

I considered using os.startfile as I have set the frozen program as needing admin, however i then can't pass arguments to it.

I also considered using subprocess.Popen but i can't work out how, or even if you can, elevate this to admin level, so while it will open the program and pass it the arguments it can't write to the registry.

Any help would be appreciated, for further information my set-up is:

  1. Windows 7 64 bit (but also plan to do XP 32 bit)
  2. python2.7.6 (again 64 bit but plan to also do 32 bit)
  3. PyUSB-1.6
  4. psutil-1.2.1
  5. cx_freeze-4.3.2

Thanks

James

After much hunting I have found a solution, I tried using:

  1. os.popen
  2. os.startfile
  3. subprocess.call
  4. subprocess.Popen

and finally os.system

as os.system is essentially the same as typing on the command line or putting the arguments into a batch file and then executing it this asks for the executables default permissions, the only downside to this is that I get a shell window when the UAC window comes up, which remains until the program it opened is closed.

the problem with the other solutions are:

1 - passes only the permissions of the calling application, regardless of what the called application requires.

2 - asks for higher level of permissions but no mechanism to pass arguments

3 - same as 1

4 - same as 1

if anyone can recommend a mechanism to prevent the shell window it would be appreciated.

James

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM