FAQ
Hi,

I have a small python script, which has been started as normal non
privileged user.

At a later point in time it would like to start another python script
with elevated privileges.

How can I write my code such, that I will get the privilege elevation
prompt and I can start a sub process / python function with elevated
privileges.

thanks for any hints.

Under Linux Ubuntu I would use sudo/gksu

Under Windows 7 I'm a little lost.

A Python only solution is of course prefered, but any .bat .c .C# .cmd
wrapper would be fine.


N

Search Discussions

  • News123 at May 1, 2010 at 3:57 pm
    I Found a first solution, though not very satisfying:
    News123 wrote:
    Hi,

    I have a small python script, which has been started as normal non
    privileged user.

    At a later point in time it would like to start another python script
    with elevated privileges.

    How can I write my code such, that I will get the privilege elevation
    prompt and I can start a sub process / python function with elevated
    privileges.

    thanks for any hints.

    Under Linux Ubuntu I would use sudo/gksu

    Under Windows 7 I'm a little lost.

    A Python only solution is of course prefered, but any .bat .c .C# .cmd
    wrapper would be fine.
    I could call following command:

    PowerShell -Command (New-Object -com
    'Shell.Application').ShellExecute('Cmd.exe', '/c
    c:\abs_path_to_my_app\pyscript.pyw', '
    ', 'runas')


    So I might do something like:

    # ########### script starts
    import subprocess

    cmd_wrap = "PowerShell -Command (New-Object -com "\
    "'Shell.Application').ShellExecute('Cmd.exe', "\
    "'/c %s', '', 'runas')"

    args = r"c:\abspathtommyapp\myapp.pyw"

    cmd = cmd_wrap % args

    # create KW args to hide console
    kwargs = { }
    su = subprocess.STARTUPINFO()
    su.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    su.wShowWindow = subprocess.SW_HIDE
    kwargs['startupinfo'] = su
    p = subprocess.Popen( cmd.split() , **kwargs )
    # script ends here


    However my issue is, that I have one annoying console window popping up
    when PowerShell starts cmd.exe


    Is there anything better?
  • Tim Golden at May 6, 2010 at 9:11 am

    On 01/05/2010 15:44, News123 wrote:
    Hi,

    I have a small python script, which has been started as normal non
    privileged user.

    At a later point in time it would like to start another python script
    with elevated privileges.

    How can I write my code such, that I will get the privilege elevation
    prompt and I can start a sub process / python function with elevated
    privileges.
    The canonical way is to use the "runas" verb from within
    ShellExecuteEx, which is exposed by the win32api module.

    TJG

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 1, '10 at 2:44p
activeMay 6, '10 at 9:11a
posts3
users2
websitepython.org

2 users in discussion

News123: 2 posts Tim Golden: 1 post

People

Translate

site design / logo © 2022 Grokbase