你知道如何在Linux中尝试一些Sudo的东西它会告诉你输入密码,当你输入时,终端窗口中没有显示任何内容(密码没有显示)?
有没有办法在Python中做到这一点?我正在编写一个需要如此敏感信息的脚本,并希望在我输入时隐藏它.
换句话说,我想从用户那里获取密码而不显示密码.
我应该如何在Python 2.7中保护我的密码?getpass.getpass()方法不起作用.在我运行代码时,密码是可见的.
我做过这样的事情:
M = imaplib.IMAP4_SSL('imap.gmail.com')
try:
emailid=input("what is your email id: ")
password=getpass.getpass("enter your password: ")
M.login(emailid,password)
Run Code Online (Sandbox Code Playgroud) 我试图创建一个程序,采取用户输入,而不是显示实际输入我想用*替换输入
我已经尝试使用此代码,但我不断收到下面的错误,我将不胜感激任何指导或帮助.
import msvcrt
import sys
def userinput(prompt='>'):
write = sys.stdout.write
for x in prompt:
msvcrt.putch(x)
entry = ""
while 1:
x = msvcrt.getch()
print(repr(x))
if x == '\r' or x == '\n':
break
if x == '\b':
entry = entry[:-1]
else:
write('*')
entry = entry + x
return entry
userEntry = userinput()
Run Code Online (Sandbox Code Playgroud)
错误:
Traceback (most recent call last):
File "C:\Users\Mehdi\Documents\Teaching\KS5\AS CS\getPass.py", line 24, in <module>
userEntry = userinput()
File "C:\Users\Mehdi\Documents\Teaching\KS5\AS CS\getPass.py", line 9, in userinput
msvcrt.putch(x)
TypeError: putch() argument …Run Code Online (Sandbox Code Playgroud) 我想问一下我们可以对输入函数进行哪些更改,以便我们输入的任何内容都表示为“*”。正如我们都看到的那样,当我们在登录页面上输入密码时,它会被写为 *** **** .这可以用 python 完成吗?像这样的东西:
Python 3.6.6 (v3.6.6:4cu1f74eh7, Jun 27 2018, 02:47:15) [MSC v.1900 64 bit
(Intel)] on win64
Type "copyright", "credits" or "license()" for more information.
>>> a = input('Write your password here:')
Write your password here: ************* # we write our password
>>> print(a)
stackoverflow
Run Code Online (Sandbox Code Playgroud)