我正在尝试创建一个受密码保护的服务器,但是当我输入密码时,输出不允许我输入密码。所有其他输入只是
getpass
不允许我输入内容。请帮助
import smtplib, json_store_client, getpass, hashlib, datetime, sys
from termcolor import colored
from time import sleep, ctime
with smtplib.SMTP('smtp.gmail.com',587) as smtp:
smtp.ehlo() #starts email procedure
smtp.starttls() #encrypts
smtp.ehlo() #starts the email procedure again
pw=getpass.getpass(colored("Enter password:\n","green"))
global attemptpw
attemptpw = 0
try:
smtp.login("********@gmail.com",pw)
#**** is my email, I just don’t want to show it
x=chat("[MOD] {} ".format(opt2),True
except:
print(colored("Invalid Admin password","red"))
sleep(1)
else:
print("password entered:",pw)
我正在使用的代码:
repl.it
修改: 我的代码在这里: repl.it/codingandmemes
答案 0 :(得分:0)
在repl.it中测试了原始代码,它可以正常工作,只需在行print(pw)
之后添加pw=getpass.getpass(colored("Enter password:\n","green"))
进行验证。
getpass
实现了无回声功能(doc here),这意味着无论您键入什么内容,它都不会打印在屏幕上,您可能希望小点(••••••)代表您输入密码的每个字符,无回声毫无意义,甚至连小点也没有。
此功能广泛地在UNIX系统上实现,以增加一点额外的安全性,这样其他人将无法分辨出密码的长度,请参见this post。