Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I read password from console in IronPython?

Status
Not open for further replies.

mikrom

Programmer
Mar 27, 2002
2,984
3
38
SK
Hello,

I need to get password from console, I tried
import getpass
pwd = getpass.getpass()
But this do not work - I get a Warning: Problem with getpass. Password may be echoed.
and really the password will be displayed on console.

I looked at System.Console which has the methods Read() and ReadLine(), but I don't know how to configure
these methods, so that they will not display the input string.
I searched on web for an example in VB.NET or C#, but I found nothing.
So, my question is: How can I read password from console in IronPython?

Regards

Roman
 
I investigated about this problem and I found the following out:
.NET has not a direct function for entering password from console without echo. (I wondered myself why, when Java has such one). But to my happiness I found an usable example in C#, how to solve this problem using System.Console.ReadKey(True) function for reading a character without echo on console
The C# example is here:
The argument True of the ReadKey() function causes that the character will not be echoed on console. The function does not return directly a character but a ConsoleKeyInfo datatype

So I wrote a simple module net_getpass which contains a function getpass(), here is it:

Code:
'''
Module net_getpass
Getting password from console in IronPython (Python.NET)

Author: Roman Miklos (RMiklos@pss.sk)
'''
import clr
import System
from System import Console


def getpass(prompt="Password:"):
  '''Read Password from Console without echo'''
  # Prompt
  Console.Write(prompt)
  # create Instance of ConsoleKeyInfo
  key = System.ConsoleKeyInfo()
  # initialize vars
  pwd =""
  EnterPressed = False
  # Read while input not ends
  while not EnterPressed:
    key = Console.ReadKey(True)
    if key.Key == System.ConsoleKey.Enter:
      # End of Input
      EnterPressed = True
      
    elif key.Key == System.ConsoleKey.Backspace:
      if len(pwd)>0:
        # Clear last character in password	      
        pwd = pwd[:-1]
        Console.Write(key.KeyChar)
        Console.Write(" ")
        Console.Write(key.KeyChar)
      else:
        Console.Beep()
	
    else:
      pwd += key.KeyChar
      Console.Write("*")
      
  # Next line
  Console.WriteLine()
  # return password
  return (pwd)

################################################################################
# Module Test
################################################################################
if __name__ == "__main__":
  print "Your Password is: %s" % getpass("Enter your password please:")

You can use this module in your Python.NET programs as ordinary

Code:
...
import net_getpass
pwd=net_getpass.getpass("Password    : ")
...

However I don't make usage of .NET SecureString() object in my module as showed in above C# example but for simplicity I use only normal python string.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top