Using Python to check if the application is running as an Administrator

Using Python to check if the application is running as an Administrator

Overview

Ever found yourself pondering whether your application has the mighty powers of administration behind it? Worry no more! Our latest post dives into a nifty Python script that seamlessly checks for admin privileges, whether you’re navigating the Linux landscape or maneuvering through the windows of, well, Windows (XP users, send your apologies). With a light-hearted approach, we offer you a quick, easy path to discerning your app’s authority level. Perfect for developers seeking cross-platform solutions, minus the headache. So, gear up and let’s decipher the realms of admin rights together!

The source below checks if the application is running with administrator privileges. Also, this script works on both Unis and Windows system… well, almost all Windows Systems… it doesnt work on WindowsXP… sorry. 🙁

import ctypes, os

def isAdmin():
    try:
        is_admin = (os.getuid() == 0)
    except AttributeError:
        is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
    return is_admin

This code first tries to check it"s running as admin in a Unix system, if that fails, an exception is happens and the script assumes you"re on a Windows System.

The function will return true or false, depending if you"re an admin or not…

Here"s how to use it:

if isAdmin():
    print("Admin! Oh yeah!")
else:
    print("Just a mortal!")

Reference:

Translations: