Python| os.getresgid() 和 os.setresgid() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
os 模块中的所有函数在文件名和路径无效或不可访问的情况下,或具有正确类型但操作系统不接受的其他参数的情况下引发OSError 。
Python中的os.getresgid()
方法用于获取当前进程的真实、有效和保存的组 id, os.setresgid()
方法用于设置当前进程的真实、有效和保存的组 id。
或者,我们也可以分别使用os.getgid()
和os.getegid()
方法获取当前进程的真实有效组 id,也可以使用os.setgid()
() 和设置当前进程的真实有效组 id 和os.setegid()
方法。
注意: os.setresgid()
和os.getresgid()
方法仅在 UNIX 平台上可用,并且os.setresgid()
方法的功能通常仅对超级用户可用,因为只有超级用户可以更改进程的组 ID。超级用户是指具有在操作系统中运行或执行任何程序的所有权限的 root 用户或管理用户。
os.getresgid() 方法——
Syntax: os.getresgid()
Parameter: No parameter is required
Return Type: This method returns a tuple whose attributes denotes real, effective, and saved group ids of the current process.
# Python program to explain os.getresgid() method
# importing os module
import os
# Get the current process’s
# real, effective, and saved group ids.
# using os.getresgid() method
rgid, egid, sgid = os.getresgid()
# Print the current process’s
# real, effective, and saved group ids.
print("Real group id of the current process:", rgid)
print("Effective group id of the current process:", egid)
print("Saved group id of the current process:", sgid)
输出:
os.setresgid() 方法——
Syntax: os.setresgid(rgid, egid, sgid)
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.
sgid: An integer value representing new saved group id for the current process.
Return Type: This method does not return any value.
# Python program to explain os.setresgid() method
# importing os module
import os
# Get the current process’s
# real, effective, and saved group ids
# using os.getresgid() method
rgid, egid, sgid = os.getresgid()
# Print the current process’s
# real, effective, and saved group ids
print("Real group id of the current process:", rgid)
print("Effective group id of the current process:", egid)
print("Saved group id of the current process:", sgid)
# Change the current process’s
# real, effective, and saved group ids
# using os.setresgid() method
rgid = 100
egid = 200
sgid = 300
os.setresgid(rgid, egid, sgid)
print("\nReal, effective, and saved group ids changed\n")
# Get the current process’s
# real, effective, and saved group ids
# using os.getresgid() method
rgid, egid, sgid = os.getresgid()
# Print the current process’s
# real, effective, and saved group ids
print("Real group id of the current process:", rgid)
print("Effective group id of the current process:", egid)
print("Saved group id of the current process:", sgid)
输出: