如何将密码回显为星号 [英] How to have password echoed as asterisks

查看:188
本文介绍了如何将密码回显为星号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何提示输入密码,并让用户输入的内容以星号(********)回显

I am trying to figure out how to prompt for a password, and have the users input echoed back as asterisks (********)

所以最近,我接受了一个创建远程服务器的项目,该服务器可以使用Python中的套接字模块连接到该服务器.尚未完成,因为我正在制作用于连接服务器的控制台.

So recently, I took on the project of creating a remote server that could be connected to using the socket module in Python. It's not yet done, as I am in the process of making the console used to connect to the server.

我正在尝试创建一个提示用户输入用户名和密码的登录窗口,尽管输入密码后,我正在寻找要打印的星号,例如常见的密码输入(即-Sekr3t被回显为:* * * * * *).

I am trying make a login window where a user is prompted to enter their Username and Password, although when the password is entered I am looking for asterisks to be printed, like common password entry (i.e. - Sekr3t is echo'd as: * * * * * *).

经过数天的研究,我几乎完成了,但是仍然有最后一个错误使我发疯(我完全无法解决).注意-我知道有一个getpass模块可以用来更轻松地替换它,但是没有任何回声不正是我想要的.

After days of research on it, I am nearly done yet there is still a last error that is driving me insane (I am completely unable to solve it). Note - I know there is a getpass module that could be used to replace this, with much greater ease, but nothing echo'd isn't exactly what I'm looking for.

这是我到目前为止拥有的密码登录代码,我无法弄清为什么它不回显星号:

Here's the password login code I have so far, and I cannot figure out why it does not echo asterisks:

import msvcrt
import sys

def login(prompt = '> '):
   write = sys.stdout.write

   for x in prompt:
       msvcrt.putch(x)
   passw = ""

   while 1:
       x = msvcrt.getch()
       if x == '\r' or x == '\n':
           break
       if x == '\b':
           # position of my error
           passw = passw[:-1]
       else:
           write('*')
           passw = passw + x
   msvcrt.putch('\r')
   msvcrt.putch('\n')
   return passw

>

如果您阅读代码和/或通过IDLE之类的代码运行代码,您可能会注意到,当用户使用退格键时,存储的数据将被删除一个字符,但不会打印出星号.即-Sekr3t +退格键= Sekr3,但* * * * * *仍然回显.我们非常感谢您提供有关在输入退格键后如何删除最后一个星号的帮助.还要注意,许多代码来自getpass模块.我知道这不是很好的解释,也许不是很好的代码,但我仍在学习中-请多多包涵.

If you read through the code and/or ran it through something like IDLE, you might notice that when the user back-space's, the stored data is erased by one character, but the asterisks printed aren't. i.e. - Sekr3t + backspace = Sekr3, yet * * * * * * is left echo'd. Any help on how to erase the last asterisk when backspace is enter'd would be greatly appreciated. Also note, a lot of this code was from the getpass module. I know it's not that great of an explanation, and probably not that great of code but I'm still learning -- please bear with me.

推荐答案

您应该可以通过写字符\x08 \x08来删除星号. \x08会将光标移回一个位置,空格将覆盖星号,然后最后一个\x08会将光标再次移回,将其放置在正确的位置以写入下一个*.

You should be able to erase an asterisk by writing the characters \x08 \x08. The \x08 will move the cursor back one position, the space will overwrite the asterisk, then the last \x08 will move the cursor back again, putting it in the correct position to write the next *.

我不知道如何确定何时键入退格键,但是您可以轻松做到这一点:只需在调用x = msvcrt.getch()之后添加print repr(x)之类的内容,然后启动程序即可然后按退格键.

I don't know off the top of my head how to determine when a backspace is typed, but you can do that easily: just add something like print repr(x) after you've called x = msvcrt.getch(), then start your program and hit backspace.

这篇关于如何将密码回显为星号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆