Python中的OS模塊提供了與操作係統進行交互的函數。操作係統屬於Python的標準實用程序模塊。該模塊提供了使用依賴於操作係統的函數的便攜式方法。
如果文件名和路徑無效或無法訪問,或者具有正確類型但操作係統不接受的其他參數,則os模塊中的所有函數都會引發OSError。
os.getresuid()
Python中的方法用於獲取當前進程的真實,有效和已保存的用戶ID,以及os.setresuid()
方法用於設置當前流程的真實,有效和已保存的用戶ID。
Unix之類的操作係統中的每個用戶都由不同的整數標識,該唯一數字稱為UserID。為流程定義了三種類型的UserID,可以根據任務的特權動態更改。
Real UserID:It is account of owner of this process. It defines which files that this process has access to.
Effective UserID:It is normally same as Real UserID, but sometimes it is changed to enable a non-privileged user to access files that can only be accessed by root.
Saved UserID:It is used when a process is running with elevated privileges (generally root) needs to do some under-privileged work, this can be achieved by temporarily switching to non-privileged account.
While performing under-privileged work, the effective UID is changed to some lower privilege value, and the euid is saved to saved userID(suid), so that it can be used for switching back to privileged account when task is completed.
注意: os.setresuid()
和os.getresuid()
方法僅在UNIX平台和函數上可用os.setresuid()
該方法通常僅對超級用戶可用,因為隻有超級用戶才能更改進程的ID。
超級用戶是指具有運行或執行操作係統中所有程序的所有權限的root用戶或管理用戶。
os.getresuid()方法-
用法: os.getresuid()
參數:不需要參數
返回類型:此方法返回一個元組,其屬性表示當前進程的真實,有效和已保存的用戶ID。
# Python program to explain os.getresuid() method
# importing os module
import os
# Get the current process’s
# real, effective, and saved user ids.
# using os.getresuid() method
ruid, euid, suid = os.getresuid()
# Print the current process’s
# real, effective, and saved user ids.
print("Real user id of the current process:", ruid)
print("Effective user id of the current process:", euid)
print("Saved user id of the current process:", suid)
輸出:
os.setresuid()方法-
用法: os.setresuid(ruid, euid, suid)
參數:
ruid:一個整數值,表示當前進程的新用戶ID。
euid:一個整數值,表示當前進程的新有效用戶ID。
suid:一個整數值,代表當前進程的新保存的用戶ID。
返回類型:此方法不返回任何值。
# Python program to explain os.setresuid() method
# importing os module
import os
# Get the current process’s
# real, effective, and saved user ids.
# using os.getresuid() method
ruid, euid, suid = os.getresuid()
# Print the current process’s
# real, effective, and saved user ids.
print("Real user id of the current process:", ruid)
print("Effective user id of the current process:", euid)
print("Saved user id of the current process:", suid)
# Change the current process’s
# real, effective, and saved user ids
# using os.setresuid() method
ruid = 100
euid = 200
suid = 300
os.setresuid(ruid, euid, suid)
print("\nReal, effective, and saved user ids changed\n")
# Get the current process’s
# real, effective, and saved user ids.
# using os.getresuid() method
ruid, euid, suid = os.getresuid()
# Print the current process’s
# real, effective, and saved user ids.
print("Real user id of the current process:", ruid)
print("Effective user id of the current process:", euid)
print("Saved user id of the current process:", suid)
輸出:
相關用法
- Python next()用法及代碼示例
- Python set()用法及代碼示例
- Python os.dup()用法及代碼示例
- Python os.WEXITSTATUS()用法及代碼示例
- Python os._exit()用法及代碼示例
- Python PIL UnsahrpMask()用法及代碼示例
- Python Numpy np.fft()用法及代碼示例
- Python os.abort()用法及代碼示例
- Python PIL RankFilter()用法及代碼示例
- Python os.WIFEXITED()用法及代碼示例
- Python os.setgroups()用法及代碼示例
- Python os.getcwd()用法及代碼示例
- Python os.sendfile()用法及代碼示例
- Python os.pipe2()用法及代碼示例
注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | os.getresuid() and os.setresuid() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。