Python| os.setregid() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
os 模块中的所有函数在文件名和路径无效或不可访问的情况下,或具有正确类型但操作系统不接受的其他参数的情况下引发OSError 。
Python中的os.setregid()
方法用于设置当前进程的真实有效组 id。但是,我们可以分别使用os.setgid()
和os.setegid()
方法分别设置当前进程的真实有效组 ID。
注意: os.setregid()
方法仅在 UNIX 平台上可用,并且此方法的功能通常仅对超级用户可用。超级用户是指具有在操作系统中运行或执行任何程序的所有权限的 root 用户或管理用户。
Syntax: os.setregid(rgid, egid)
Parameters:
rgid: An integer value representing new group id for the current process.
egid: An integer value representing new effective group id for the current process.
Return Type: This method does not return any value.
代码: 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)
输出: