silent raw_input for passwords

Dan Bishop danb_83 at yahoo.com
Sat May 1 04:29:20 EDT 2004


Stephen Boulet <stephen.no at spam.theboulets.net.please> wrote in message news:<B36dncCgsNfVkQ7d3cwC-g at speakeasy.net>...
> I need a password for a script and I would like to not have it stored in a
> file or shown in a terminal.
> 
> "passphrase = raw_input()" still lets you see the input on the screen. Is
> there a way to have it be hidden? It's my gpg passphrase, so I don't want
> it anywhere except in my head.

There's probably an easier way, but you can use:

def input_password(echo=True):
   chars = []
   while True:
      newChar = getch()
      if newChar in '\r\n':
         break
      elif newChar in '\b\x7F':
         if chars:
            del chars[-1]
         sys.stdout.write('\b')
      else:
         chars.append(newChar)
         if echo:
            sys.stdout.write('*')
   return ''.join(chars)



More information about the Python-list mailing list