sebthom.de

Menu
  • Home
  • About me
  • Imprint
    • Datenschutzerklärung
  • Guestbook
Menu

getpass for Jython

Posted on 2008-11-212012-12-18 by sebthom

In my current project I am developing some Jython based command line tools and had the need for masking passwords entered in the command shell. Python provides the getpass module for this purpose. Unfortunately this module has not been made available for Jython.

Here comes a module I wrote to provide this kind of functionality. It uses the mechanism described here //java.sun.com/developer/technicalArticles/Security/pwordmask/ to mask characters entered at the command prompt. Additionally if Java 6 or higher is running Jython the module will instead use the new Console.readPassword() method. In case the module is running in an environment where the getpass module is available it will delegate to the corresponding methods of this module instead of using its own implementations.

# ext_getpass.py
# @author Sebastian Thomschke, //sebthom.de/
import thread, sys, time, os

# exposed methods:
__all__ = ["getpass","getuser"]

def __doMasking(stream):
    __doMasking.stop = 0
    while not __doMasking.stop:
        stream.write("b*")
        stream.flush()
        time.sleep(0.01)

def generic_getpass(prompt="Password: ", stream=None):
    if not stream:
        stream = sys.stderr
    prompt = str(prompt)
    if prompt:
        stream.write(prompt)
        stream.flush()
    thread.start_new_thread(__doMasking, (stream,))
    password = raw_input()
    __doMasking.stop = 1
    return password

def generic_getuser():
    for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
        usr = os.environ.get(name)
        if usr: return usr

def java6_getpass(prompt="Password: ", stream=None):
    if not stream:
        stream = sys.stderr
    prompt = str(prompt)
    if prompt:
        stream.write(prompt)
        stream.flush()

    from java.lang import System
    console = System.console()
    if console == None:
	    return generic_getpass(prompt, stream)
    else:
        return "".join(console.readPassword())

try:
    # trying to use Python's getpass implementation
    import getpass
    getpass = getpass.getpass
    getuser = getpass.getuser
except:
    getuser = generic_getuser
	
    # trying to use Java 6's Console.readPassword() implementation
    try:
        from java.io import Console
        getpass = java6_getpass
    except ImportError, e:
        # use the generic getpass implementation
        getpass = generic_getpass

Here is an usage example:

import ext_getpass as getpass

pw = getpass.getpass("Please enter your password:")
print "The entered password was: " + pw

1 thought on “getpass for Jython”

  1. Shawn Brown says:
    10 November 2009 at 4:11 PM

    With Jython 2.5.1, I get an AttributeError on line 51. Since that block only handles the ImportError, the script was coughing-up a traceback. In my local copy, I’ve changed line 51 to read…

    except (ImportError, AttributeError), e:

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Language

    English English    Deutsch Deutsch    

Categories

  • IT (25)
    • Development (15)
      • Java (7)
      • Jython (3)
      • Visual Basic (5)
    • Freeware Tips (1)
    • Linux (3)
    • WebSphere Portal (2)
    • Windows (1)
    • Wordpress (4)
  • My Freeware (2)
  • My Music (3)

Recent Posts

  • [Solved] Windows 7 “Safely Remove Hardware” pop-up menu horrendously slow
  • Bash: Capturing stderr in a variable while still printing to the console.
  • Configuring EMF Teneo with Hibernate, Commons DBCP, Spring Hibernate Transaction Manager, and the OpenSessionInViewFilter
  • Using EMF ECore model objects with Wicket components
  • Installing Tomcat 6 on Debian Squeeze
  • Leveraging PyDev’s auto-completion for indirectly created objects
  • OVal 1.40 released
  • Installing WebSphere Portal in a local network
  • Comparing version numbers in Jython / Python
  • Running TomCat 6 in Debug Mode under Windows

Blogroll

  • E L S U A
  • elektrofever.de
  • OVal
  • Sweettt.com
  • Twins’ Running Blog

Recent Comments

  • Luis Diego Villarreal on Excel – VBA Timer Example v1.0.1
  • jonathan on Guestbook
  • Pierre on Lotus Notes’ [Send only] and [Send and File] buttons for Outlook 2003
  • Yereverluvinunclebert on MyPad v1.1.6 – a PHP Editor
  • Sebastian Thomschke on MyPad v1.1.6 – a PHP Editor

Archives

  • May 2012
  • January 2011
  • October 2010
  • September 2010
  • March 2010
  • February 2010
  • September 2009
  • July 2009
  • March 2009
  • February 2009
  • November 2008
  • September 2008
  • May 2008
  • April 2008
  • September 2007
  • July 2007
  • July 2004
  • March 2003
  • August 2002
  • April 2002
  • January 2002
© 2021 sebthom.de | Powered by Minimalist Blog WordPress Theme