×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How can I read password from console in IronPython?

How can I read password from console in IronPython?

How can I read password from console in IronPython?

(OP)
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

RE: How can I read password from console in IronPython?

(OP)
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:
http://www.harper.no/valery/content/binary/ProtectData.cs.txt
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close