问题
I have an OS X Python application that uses wxPython for it's GUI controls. I'm looking to prompt the user for administrative rights (akin to using the Authorization Service API in Objective-C) before starting a network service.
The closest library I have found is Bob Ippolito's Authorization library but it is fairly outdated and has compatibility issues with Snow Leopard (OS X 10.6.4 / Python 2.6.4).
My workaround would be to create an Objective-C launcher that runs the python application with administrative rights but that feels fairly kludgy.
Thanks for the help!
回答1:
You could run your entire script with admin privileges by making it into a native Mac OS X application using Platypus.
回答2:
If it's just a shell command that you need to run, requesting elevated privileges, then you can do this:
os.system("""osascript -e 'do shell script "<commands go here>" " with administrator privileges'""")
It will run a shell command within the applescript interpreter and prompt the user for their account credentials; effectively running the command using sudo.
Note that you'll have fun escaping quotes within your command, e.g.
os.system("""osascript -e 'do shell script "mkdir -m 0775 -p \\"%s\\" " with administrator privileges'"""%d)
Hope that helps!
回答3:
You might want to check the answer to another question posted here: What kind of API should I use to elevate user privilege to super-user in a Mac OS X?
来源:https://stackoverflow.com/questions/3170771/mac-os-x-python-gui-administrator-prompt