问题:在Python中获取命令行密码输入

您知道在Linux中尝试一些Sudo东西时如何告诉您输入密码,并且在键入时,终端窗口中什么都没有显示(密码未显示)?

有没有办法在Python中做到这一点?我正在编写一个脚本,该脚本需要非常敏感的信息,并希望在键入时将其隐藏。

换句话说,我想从用户那里获取密码而不显示密码。

You know how in Linux when you try some Sudo stuff it tells you to enter the password and, as you type, nothing is shown in the terminal window (the password is not shown)?

Is there a way to do that in Python? I’m working on a script that requires so sensitive info and would like for it to be hidden when I’m typing it.

In other words, I want to get the password from the user without showing the password.


回答 0

使用getpass.getpass()

from getpass import getpass
password = getpass()

可以将可选提示作为参数传递;默认值为"Password: "

请注意,此功能需要正确的终端,因此它可以关闭键入字符的回显– 有关更多详细信息,请从IDLE运行时,请参见“ GetPassWarning:无法控制终端上的回显”

Use getpass.getpass():

from getpass import getpass
password = getpass()

An optional prompt can be passed as parameter; the default is "Password: ".

Note that this function requires a proper terminal, so it can turn off echoing of typed characters – see “GetPassWarning: Can not control echo on the terminal” when running from IDLE for further details.


回答 1

import getpass

pswd = getpass.getpass('Password:')

getpass可在Linux,Windows和Mac上使用。

import getpass

pswd = getpass.getpass('Password:')

getpass works on Linux, Windows, and Mac.


回答 2

为此使用getpass

getpass.getpass-提示用户输入密码而不回显

Use getpass for this purpose.

getpass.getpass – Prompt the user for a password without echoing


回答 3

此代码将打印一个星号,而不是每个字母。

import sys
import msvcrt

passwor = ''
while True:
    x = msvcrt.getch()
    if x == '\r':
        break
    sys.stdout.write('*')
    passwor +=x

print '\n'+passwor

This code will print an asterisk instead of every letter.

import sys
import msvcrt

passwor = ''
while True:
    x = msvcrt.getch()
    if x == '\r':
        break
    sys.stdout.write('*')
    passwor +=x

print '\n'+passwor

回答 4

15.7。getpass —便携式密码输入

#!/usr/bin/python3
from getpass import getpass
passwd = getpass("password: ")
print(passwd)

你可以在这里阅读更多

15.7. getpass — Portable password input

#!/usr/bin/python3
from getpass import getpass
passwd = getpass("password: ")
print(passwd)

You can read more here


回答 5

更新@Ahmed ALaa的答案

# import msvcrt
import getch

def getPass():
    passwor = ''
    while True:
        x = getch.getch()
        # x = msvcrt.getch().decode("utf-8")
        if x == '\r' or x == '\n':
            break
        print('*', end='', flush=True)
        passwor +=x
    return passwor

print("\nout=", getPass())

msvcrt仅适用于Windows,但从PyPI获取getch应该对两者都适用(我仅在linux上进行过测试)。您也可以注释/取消注释这两行,以使其适用于Windows。

Updating on the answer of @Ahmed ALaa

# import msvcrt
import getch

def getPass():
    passwor = ''
    while True:
        x = getch.getch()
        # x = msvcrt.getch().decode("utf-8")
        if x == '\r' or x == '\n':
            break
        print('*', end='', flush=True)
        passwor +=x
    return passwor

print("\nout=", getPass())

msvcrt us only for windows, but getch from PyPI should work for both (I only tested with linux). You can also comment/uncomment the two lines to make it work for windows.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。