【问题标题】:Is there easy way to prevent echo from input?有没有简单的方法来防止输入回声?
【发布时间】:2013-07-08 23:00:49
【问题描述】:

如何防止输入回显??

尝试过“getpass()”,但没有成功。

在 Windows IDLE 上,它不起作用

    Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import getpass
>>> p = getpass.getpass(prompt="Input: ")
Warning: Password input may be echoed.
Input: abc <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< It still echos..

在Windows的终端上,它可以工作

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import getpass
>>> p = getpass.getpass(prompt="Input: ")
Input:
>>>

有没有一种简单的方法可以防止输入回显?

【问题讨论】:

  • Windows 和 Windows 上的终端有何不同?
  • 我相信一个是IDLE,另一个是终端。
  • 我认为这个问题需要澄清一下,如果是IDLE,至少应该在问题中提及。
  • 是的,第一个在从IDLE启动的Shell中,第二个在从命令行启动的Shell中。两个都在同一台电脑上,我还以为是同一个python shell呢。
  • 我放弃了.... 在 Python 3.3.x 上,没有简单的解决方法可以在没有回显的情况下获得输入。和空闲外壳。不敢相信,但确实如此! :-(

标签: python python-3.x


【解决方案1】:

我假设您的第一个示例在 IDLE 中。

来自 getpass.win_getpass():

if sys.stdin is not sys.__stdin__:
    return fallback_getpass(prompt, stream)

IDLE 将 sys.stdin 替换为不同的对象。 getpass 检测到有人包装了标准输入,但出于安全原因而失败。

见:http://bugs.python.org/issue9290

【讨论】:

  • win_getpass 使用了 getch() 的函数,但在 IDLE 环境下无法使用。这样就不能在 IDLE 上关闭回声。
  • IDLE 是一个开发环境。解决方法是不要在生产中的 IDLE 中运行安全应用程序。我的意思是,任何可以读取本地回显的人都可以闯入解释器并遍历模块中的变量,直到找到密码!
  • 我知道IDEL是一个开发环境。但是,如果程序不能在 DEV 中运行,我们怎么知道它是否可以在 PRD 中运行!! 问题来了!!如果开发环境可以充当生产环境,那么开发会更容易,不是吗。 我们得到的就是我们得到的,无论是 DEV 还是 PRD 如果是这样,那么开发会更容易。
猜你喜欢
  • 2018-09-10
  • 2021-11-25
  • 2019-07-21
  • 1970-01-01
  • 2018-01-07
  • 2010-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多