[IronPython] Problem with getpass.getpass() in IronPython
Roman Miklos
RMiklos at pss.sk
Mon Oct 16 14:06:47 CEST 2006
With IronPython, this
import getpass
pwd = getpass.getpass()
do'nt work properly, because the password will be echoed.
I worked about this problem. The NET-platform probably doesn't have a
direct function for getting password from console.
I found only this C# example
http://www.harper.no/valery/content/binary/ProtectData.cs.txt
and wrote a simple module net_getpass.py which contains a function
getpass().
For those who have the same problem, here is the source:
------------------------------------------------------------ source
-------------------------------------------------------------------------------
'''
Module net_getpass
Getting password from console in IronPython (Python.NET)
Author: Roman Miklos (RMiklos at 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:")
------------------------------------------------------------------ end of
source
-------------------------------------------------------------------------
However I don't make usage of .NET SecureString() object in my module as
showed in above C# example,
but for simplicity I used only normal python string.
Mgr. Ing. Roman MIKLÓŠ
Prvá stavebná sporiteľňa a.s.
Bajkalská 30, P. O. Box 48
829 48 Bratislava 25
Tel.: +421/ 2 / 582 31 174
Fax: +421/ 2 / 582 31 109
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20061016/82037f8e/attachment.html>
More information about the Ironpython-users
mailing list