3

I'm using python 2.7.9 on Windows 7. I need to obtain a password from a user in order to connect to a resource. It seems that userpass = getpass.getpass("Password :") is the de facto standard for this in Python, however in Windows 7, the inputted text is just happily echoed to the output screen.

How can I mask the user input in the console or whatever when running my Python script? Thank you

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
Koko
  • 459
  • 4
  • 14
  • maybe this can help: https://stackoverflow.com/a/510364/2625645 – Max Deepfield Aug 18 '17 at 15:34
  • Do you get the message: "Warning: Problem with getpass. Passwords may be echoed."??? – Dalen Aug 18 '17 at 15:54
  • There is an activestate recipe [here](http://code.activestate.com/recipes/579148-add-password-masking-ability-to-getpassgetpass/). – Gribouillis Aug 18 '17 at 16:05
  • There is no general way to do this for "whatever" console, terminal, or input window is being used. On Windows, `getpass.getpass` can read without echo from the Windows console, but its test for switching to the fallback is horrible: i.e. `sys.stdin is not sys.__stdin__`. Are you trying to read from the console? – Eryk Sun Aug 18 '17 at 16:22
  • @Gribouillis - thanks, I was hopeful but seems that the recipe has some error in it as it still echoes the typed characters... still debugging it... – Koko Aug 18 '17 at 16:29
  • @eryksun - yes. I'm protoyping a mysql DB connection in a DOS window so .. STDIN equivalent is not equivalent I take it. – Koko Aug 18 '17 at 16:31
  • 1
    You mean a console window. Python hasn't supported DOS for many years. Anyway, are you replacing `sys.stdin`? If so, that explains why it's using the fallback that echoes user input. – Eryk Sun Aug 18 '17 at 16:41
  • Ok, I was running from within IDLE, hitting [F5]. I launched it today in a DOS/CMD window and it properly masks the password. Thanks for all the help. – Koko Aug 21 '17 at 17:22

0 Answers0