有各種各樣的 Python 程序,在這些程序中,我們必須使用密鑰、密碼短語或密碼進行秘密交易,或者識別被授權執行某些活動的用戶。在接受鍵的過程中,必須採取各種措施,不應該將該短語回顯到屏幕上,禁止語句回顯等等。Python 編程語言中的 getpass 模塊提供了所有這些特性。
在接下來的教程中,我們將藉助不同的示例詳細討論 Python getpass 模塊,並了解其用途。
了解 Python getpass
模塊
類似於 NumPy 和 Matplotlib 模塊, getpass 也是 Python 編程語言的一個模塊。 getpass 模塊要求用戶輸入密碼而不回應,這意味著不顯示用戶正在輸入的內容。
每當我們使用基於具有不同安全憑證的終端的應用,該終端在應用執行之前使用密碼,那麼它將使用 Python getpass 模塊來完成。
Python 標準庫的 getpass 模塊大致定義了兩個函數。只要基於終端的應用必須在用戶憑證驗證之後執行,這些功能就可以支持。
這些功能如下:
- getpass() 功能
- getuser() 功能
理解 getpass()函數
在各種程序中,我們有安全的數據或程序,在這種情況下,我們利用一些秘密或密鑰或密碼來識別用戶。使用 getpass() 函數,我們可以在 Python 程序中接受密碼。
讓我們用下面的例子來理解這一點:
示例:
# importing the getpass module
import getpass
# using the getpass() function
my_pass = getpass.getpass(prompt = 'Input the Password:')
# using the if-else conditional statments
if my_pass == 'Admin':
print('The key is Unlocked!')
else:
print('You\'ve entered an invalid password!')
輸出:
Input the Password:
# The inserted value is Admin
The key is Unlocked!
說明:
在上面的代碼片段中,我們導入了 getpass 模塊,並創建了一個變數,該變數使用 getpass 模塊的 getpass() 函數來存儲鍵值。然後,我們使用 if-else 條件語句來檢查輸入值是否與變數中的值匹配,如果匹配,則為用戶解鎖。
現在讓我們藉助例子來理解 getpass() 函數的不同用法。
getpass()函數,沒有提示
在下面的示例中,我們將了解如何從用戶那裡獲取密碼,並在沒有提示的情況下返回相同的密碼。
示例:
# importing the getpass module
import getpass
# using the getpass() function without prompt
my_pass = getpass.getpass()
# printing the statement
print("Entered password:", my_pass)
輸出:
Password:
# The inserted value is Admin
Entered password: Admin
說明:
在上面的代碼片段中,我們已經導入了 getpass 模塊,並定義了一個使用 getpass() 的參數。這裡,我們沒有指定任何參數。然後,我們為用戶列印了一份聲明。
帶提示的 getpass()函數
如果用戶在登錄前需要一些信息,如安全問題,我們將利用 getpass() 功能中的提示屬性。
讓我們考慮下面的例子來理解同樣的事情。
示例:
# importing the getpass module
import getpass
# using the getpass() function with prompt
my_pass = getpass.getpass(prompt = "What is your birthplace?:")
# using the if-else conditional statement
if my_pass == 'Ohio':
print("Unlock! Welcome back Dani.")
else:
print("Your password is invalid!")
輸出:
What is your birthplace?:
# The inserted value is Ohio
Unlock! Welcome back Dani.
說明:
在上面的代碼片段中,我們已經導入了 getpass 模塊。然後我們定義了一個變數,該變數使用 getpass() 函數和提示參數來包含一些消息。然後,我們使用 if-else 條件語句檢查輸入的密碼值是否與程序中存儲的值匹配,並列印相應的語句。
getpass()函數與其他流一起使用
該功能使程序員能夠流式傳輸用戶輸入的密碼。讓我們藉助下面給出的例子來理解這一點:
示例:
# importing the getpass and sys modules
import getpass
import sys
# using the getpass() function with stream
my_pass = getpass.getpass(stream = sys.stderr)
# printing the statement
print("Entered Password:", my_pass)
輸出:
Password:
# The inserted value is Admin
Entered Password: Admin
說明:
在上面的代碼片段中,我們已經導入了 getpass 模塊和 sys 模塊。然後我們定義了一個使用 getpass() 函數的變數,該函數接受 sys.stderr 流作為函數的參數。然後,我們列印了一份聲明,向用戶詢問密碼。
理解 getuser()函數
getuser() 函數返回用戶的系統登錄名。這個函數檢查計算機的環境變數。它獲取用戶的名稱並返回一個字元串,如果找不到環境變數,就會引發異常。
讓我們考慮一個基於 getuser() 函數檢索計算機用戶名的例子。
示例:
# importing the getpass and sys modules
import getpass
# using the getuser() function
my_user = getpass.getuser()
# printing the value
print("Username:", my_user)
輸出:
Username: Mango
說明:
在上面的代碼片段中,我們已經導入了 getpass 模塊,並使用了 getuser() 函數來檢索存儲在系統中的用戶名。
現在,讓我們考慮另一個使用 getuser() 和 getpass() 函數獲取用戶名和密碼的例子。
示例:
# importing the getpass module
import getpass
# using the getuser() and getpass() functions
my_user = getpass.getuser()
my_pass = getpass.getpass("Enter Password:")
# printing the values
print("Username:", my_user)
print("Password:", my_pass)
輸出:
Enter Password:
# The inserted value is Admin
Username: Mango
Password: Admin
說明:
在上面的代碼片段中,我們已經導入了 getpass 模塊,並定義了使用 getuser() 和 getpass() 函數獲取用戶名和密碼的變數。然後,我們為用戶列印了這些值。
原創文章,作者:QY8KH,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/129839.html