def setugid(user):
 try:
  from pwd import getpwuid
  passwd = getpwuid(int(user))
 except ValueError:
  from pwd import getpwnam
  passwd = getpwnam(user)
 if hasattr(os, 'initgroups'):
  os.initgroups(passwd.pw_name, passwd.pw_gid)
 else:
  import ctypes
  if (ctypes.CDLL(None).initgroups(passwd.pw_name, passwd.pw_gid) < 0):
   err = ctypes.c_int.in_dll(ctypes.pythonapi, 'errno').value
   raise OSError(err, os.strerror(err), 'initgroups')
 os.setgid(passwd.pw_gid)
 os.setuid(passwd.pw_uid)
 os.environ['HOME'] = passwd.pw_dir

def setugid ( user ) : try : from pwd import getpwuid passwd = getpwuid ( int ( user ) ) except ValueError : from pwd import getpwnam passwd = getpwnam ( user ) if hasattr ( os , 'initgroups' ) : os . initgroups ( passwd . pw_name , passwd . pw_gid ) else : import ctypes if ( ctypes . CDLL ( None ) . initgroups ( passwd . pw_name , passwd . pw_gid ) < 0 ) : err = ctypes . c_int . in_dll ( ctypes . pythonapi , 'errno' ) . value raise OSError ( err , os . strerror ( err ) , 'initgroups' ) os . setgid ( passwd . pw_gid ) os . setuid ( passwd . pw_uid ) os . environ [ 'HOME' ] = passwd . pw_dir
change process user and group id argument is a numeric user id or a user name .