English 中文(简体)
如何读到呼应“*”的序号?
原标题:How to read password with echo "*" in Python console program?

I m writing a console program with Python under Windows.
The user need to login to use the program, when he input his password, I d like they to be echoed as "*", while I can get what the user input.
I found in the standard library a module called getpass, but it will not echo anything when you input(linux like).
Thanks.

最佳回答

。 你们可以很容易地加以修改,以便做到这一点。 事实上,这里是一份经过修改的“getpass.win_getpass()文本,你可以将其贴在你的法典中:

import sys

def win_getpass(prompt= Password:  , stream=None):
    """Prompt for password with echo off, using Windows getch()."""
    import msvcrt
    for c in prompt:
        msvcrt.putch(c)
    pw = ""
    while 1:
        c = msvcrt.getch()
        if c ==  
  or c ==  
 :
            break
        if c ==  03 :
            raise KeyboardInterrupt
        if c ==   :
            pw = pw[:-1]
            msvcrt.putch(  )
        else:
            pw = pw + c
            msvcrt.putch("*")
    msvcrt.putch( 
 )
    msvcrt.putch( 
 )
    return pw

You might want to reconsider this, however. The Linux way is better; even just knowing the number of characters in a password is a significant hint to someone who wants to crack it.

问题回答

友好的答案是很接近的,但问题在于后天空,不能抹掉星号,而后天空能够回落到投入之外。

Try:

def win_getpass(prompt= Password:  , stream=None):
    """Prompt for password with echo off, using Windows getch()."""
    if sys.stdin is not sys.__stdin__:
        return fallback_getpass(prompt, stream)
    import msvcrt
    for c in prompt:
        msvcrt.putwch(c)
    pw = ""
    while 1:
        c = msvcrt.getwch()
        if c ==  
  or c ==  
 :
            break
        if c ==  03 :
            raise KeyboardInterrupt
        if c ==   :
            if pw ==   :
                pass
            else:
                pw = pw[:-1]
                msvcrt.putwch(  )
                msvcrt.putwch(" ")
                msvcrt.putwch(  )
        else:
            pw = pw + c
            msvcrt.putwch("*")
    msvcrt.putwch( 
 )
    msvcrt.putwch( 
 )
    return pw

注 put不与 p2.x合作,你需要使用 m。 相反。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签