【问题标题】:SMTP getpass.getpass() not accepting input, can you fix this?SMTP getpass.getpass() 不接受输入,你能解决这个问题吗?
【发布时间】:2020-03-10 19:37:03
【问题描述】:

我正在尝试创建受密码保护的服务器,但是当我输入密码时,输出不允许我输入密码。所有其他输入都可以正常工作 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

【问题讨论】:

  • 是否打印“输入密码:”?大多数情况下,使用getpass 会给你回声免费提示。
  • @Foocli,“输入密码:”打印为绿色,是的。但我会试试的
  • 刚刚测试了没有括号内文字get pass.getpass(colored(string, color))的代码,还是不行。

标签: python smtp getpass


【解决方案1】:

在 repl.it 中测试了您的原始代码,它可以正常工作,只需在 pw=getpass.getpass(colored("Enter password:\n","green")) 行之后添加 print(pw) 即可验证。

getpass 实现了无回声功能(doc here),这意味着无论您键入什么,它都不会打印在屏幕上,您可能希望小点 (••••••) 代表您输入的每个字符输入密码,没有回声意味着什么,甚至是小点。

这是在 unix 系统上广泛实施的一项功能,用于增加一点额外的安全性,以便其他人无法知道您的密码长度,请参阅this post

【讨论】:

  • 谢谢@Foocli,但有没有办法回显密码?另外,我输入了正确的密码,它不接受它
  • 我还有一个项目想让你看看(here)
  • 至于你的第一个问题,getpass 完全隐藏密码如果终端支持的话,如果你想打印星号让用户知道他们输入的长度,你可能想看看其他库,例如this post 建议stdiomask;或者如果您想直接打印密码,请使用input
  • @CodingAndMemes 如果我有帮助,请考虑接受答案,对于您的第二个问题,开始一个新问题并将您的代码直接发布在问题中,我刚刚检查过,第 86 行现在是一个简单的打印语句,我无法重现该错误。您不必使用这么多外部库来实现您的代码,例如,print in python can flush the output 和 termcolor last updated at Jan 2011...如果遇到问题,这可能是个问题。
猜你喜欢
  • 1970-01-01
  • 2011-01-24
  • 2021-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多