I am trying to figure out how to prompt for a password, and have the users input echoed back as asterisks (********
)
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.
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: * * * * * *).
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
>
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.