Since the Raspberry Pi was released, the python programming got a new life.
So, many customers ask to me how to do some simple task using this programming/scripting language.
Checking for root is useful if you want to check whether a user has the privilege to bind a socket to a port less than 1024. In this case, you can generate a useful error message beforehand, rather than just trying and having the bind command fail.
In python is a really simple task,
just add the following few lines to your program:
import os, sys
# if not root...kick out
if not os.geteuid()==0:
sys.exit("\nOnly root can run this script\n")
Please, take care of the following notes.
If you don't want a user can execute your script you shall issue the following bash command on your script:
$ chown root script.py
$ chmod 700 script.py