How to have password echoed as asterisks

后端 未结 2 416
我在风中等你
我在风中等你 2020-11-27 08:04

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

相关标签:
2条回答
  • 2020-11-27 08:10

    I think this way is very simple and effective

    import sys
    import msvcrt
    
    passwor = ''
    while True:
        x = msvcrt.getch()
        if x == '\r':
            break
        sys.stdout.write('*')
        passwor +=x
    
    print '\n'+passwor
    
    0 讨论(0)
  • 2020-11-27 08:20

    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 *.

    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.

    0 讨论(0)
提交回复
热议问题