Python os.setregid()
Python os模块中的所有函数在文件名和路径无效或不可访问,或其他具有正确类型但操作系统不接受的参数时都会引发OSError。
Python中的os.setregid()方法用于设置当前进程的真实有效的组id。但是,我们可以分别使用os.setgid()和os.setegid()方法来设置当前进程的真实有效的组id。
注意:os.setregid()方法只在UNIX平台上可用,而且此方法的功能通常只对超级用户可用。超级用户是指具有运行或执行操作系统中任何程序的所有权限的根用户或管理用户。
语法:os.setregid(rgid, egid)
参数:
rgid:整数值,表示当前进程的新组id。
egid:整数值,表示当前进程的新有效组id。
返回类型:此方法不返回任何值。
示例1
使用os.setregid()方法
# Python program to explain os.setregid() method
# importing os module
import os
# Get the current process’s
# real group id
# using os.getgid() method
rgid = os.getgid()
# Get the current process’s
# effective group id.
# using os.getegid() method
egid = os.getegid()
# Print the current process’s
# real and effective group ids.
print("Real group id of the current process:", rgid)
print("Effective group id of the current process:", egid)
# Change the current process’s
# real and effective group ids
# using os.setregid() method
rgid = 100
egid = 200
os.setregid(rgid, egid)
print("\nReal and effective group ids changed\n")
# Get the current process’s
# real and effective group ids
rgid = os.getgid()
egid = os.getegid()
# Print the current process’s
# real and effective group ids.
print("Real group id of the current process:", rgid)
print("Effective group id of the current process:", egid)
# We can also use os.setgid() and
# os.setegid() method to set the
# current process’s real and
# effective group ids respectively
# Change the current process’s
# real group id
# using os.setgid() method
rgid = 300
os.setgid(rgid)
# Change the current process’s
# effective group id
# using os.setegid() method
egid = 400
os.setegid(egid)
print("\nReal and effective group ids changed\n")
# Print the current process’s
# real and effective group ids.
print("Real group id of the current process:", rgid)
print("Effective group id of the current process:", egid)
输出: