首页 > 编程知识 正文

Python 中的getpass模块

时间:2023-11-26 13:13:13 阅读:309404 作者:SZEJ

有各种各样的 Python 程序,在这些程序中,我们必须使用密钥、密码短语或密码进行秘密交易,或者识别被授权执行某些活动的用户。在接受键的过程中,必须采取各种措施,不应该将该短语回显到屏幕上,禁止语句回显等等。Python 编程语言中的 getpass 模块提供了所有这些特性。

在接下来的教程中,我们将借助不同的示例详细讨论 Python getpass 模块,并了解其用途。

了解 Python getpass模块

类似于 NumPy 和 Matplotlib 模块, getpass 也是 Python 编程语言的一个模块。 getpass 模块要求用户输入密码而不回应,这意味着不显示用户正在输入的内容。

每当我们使用基于具有不同安全凭证的终端的应用,该终端在应用执行之前使用密码,那么它将使用 Python getpass 模块来完成。

Python 标准库的 getpass 模块大致定义了两个函数。只要基于终端的应用必须在用户凭证验证之后执行,这些功能就可以支持。

这些功能如下:

  1. getpass() 功能
  2. 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() 函数获取用户名和密码的变量。然后,我们为用户打印了这些值。


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。