{article Dive into Python}{title} {text} {/article}

Supporting Platform−Specific Functionality

>>> def win_getpass(prompt='Password: ', stream=None):
"""Prompt for password with echo off, using Windows getch()."""
if sys.stdin is not sys.__stdin__:
return default_getpass(prompt)
import msvcrt
for c in prompt:
msvcrt.putch(c)
pw = ""
while 1:
c = msvcrt.getch()
if c == '\r' or c == '\n':
break
if c == '\003':
raise KeyboardInterrupt
if c == '\b':
pw = pw[:-1]
else:
pw = pw + c
msvcrt.putch('\r')
msvcrt.putch('\n')
return pw

>>> try:
import termios, TERMIOS
except ImportError:
try:
import msvcrt
except ImportError:
try:
from EasyDialogs import AskPassword
except ImportError:
getpass = default_getpass
else:
getpass = AskPassword
else:
getpass = win_getpass
else:
getpass = unix_getpass

>>>
termios is a UNIX−specific module that provides low−level control over the input terminal. If this module is
not available (because it's not on your system, or your system doesn't support it), the import fails and Python
raises an ImportError, which you catch.

OK, you didn't have termios, so let's try msvcrt, which is a Windows−specific module that provides an
API to many useful functions in the Microsoft Visual C++ runtime services. If this import fails, Python will
raise an ImportError, which you catch.

If the first two didn't work, you try to import a function from EasyDialogs, which is a Mac OS−specific
module that provides functions to pop up dialog boxes of various types. Once again, if this import fails, Python
will raise an ImportError, which you catch.

None of these platform−specific modules is available (which is possible, since Python has been ported to a lot
of different platforms), so you need to fall back on a default password input function (which is defined
elsewhere in the getpass module). Notice what you're doing here: assigning the function
default_getpass to the variable getpass. If you read the official getpass documentation, it tells you
that the getpass module defines a getpass function. It does this by binding getpass to the correct
function for your platform. Then when you call the getpass function, you're really calling a
platform−specific function that this code has set up for you. You don't need to know or care which platform
your code is running on −− just call getpass, and it will always do the right thing.

A try...except block can have an else clause, like an if statement. If no exception is raised during the
try block, the else clause is executed afterwards. In this case, that means that the from EasyDialogs
import AskPassword import worked, so you should bind getpass to the AskPassword function.
Each of the other try...except blocks has similar else clauses to bind getpass to the appropriate
function when you find an import that works.

Python 3.4.1 Shell

{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
>>> def win_getpass(prompt='Password: ', stream=None):
        """Prompt for password with echo off, using Windows getch()."""
        if sys.stdin is not sys.__stdin__:
            return default_getpass(prompt)
        import msvcrt
        for c in prompt:
            msvcrt.putch(c)
        pw = ""
        while 1:
            c = msvcrt.getch()
            if c == '\r' or c == '\n':
                break
            if c == '\003':
                raise KeyboardInterrupt
            if c == '\b':
                pw = pw[:-1]
            else:
                pw = pw + c
        msvcrt.putch('\r')
        msvcrt.putch('\n')
        return pw

>>> try:
    import termios, TERMIOS
except ImportError:
    try:
        import msvcrt
    except ImportError:
        try:
            from EasyDialogs import AskPassword
        except ImportError:
            getpass = default_getpass
        else:
            getpass = AskPassword
    else:
        getpass = win_getpass
else:
    getpass = unix_getpass


>>>


</pre>

<script language="javascript" type="text/javascript">
    // You can place JavaScript like this

</script>
<?php
    // You can place PHP like this

?>
{/source}

Edit Window

wingetpass.py

{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
# Save as wingetpass.py
def win_getpass(prompt='Password: ', stream=None):
        """Prompt for password with echo off, using Windows getch()."""
        if sys.stdin is not sys.__stdin__:
            return default_getpass(prompt)
        import msvcrt
        for c in prompt:
            msvcrt.putch(c)
        pw = ""
        while 1:
            c = msvcrt.getch()
            if c == '\r' or c == '\n':
                break
            if c == '\003':
                raise KeyboardInterrupt
            if c == '\b':
                pw = pw[:-1]
            else:
                pw = pw + c
        msvcrt.putch('\r')
        msvcrt.putch('\n')
        return pw

try:
    import termios, TERMIOS
except ImportError:
    try:
        import msvcrt
    except ImportError:
        try:
            from EasyDialogs import AskPassword
        except ImportError:
            getpass = default_getpass
        else:
            getpass = AskPassword
    else:
        getpass = win_getpass
else:
    getpass = unix_getpass



</pre>

<script language="javascript" type="text/javascript">
    // You can place JavaScript like this

</script>
<?php
    // You can place PHP like this

?>
{/source}

For a copy of getpass.py

Link to

Apple Open Source Browser

Edit Window

getpass.py

{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
# Save as getpass.py
"""Utilities to get a password and/or the current user name.

getpass(prompt) - prompt for a password, with echo turned off
getuser() - get the user name from the environment or password database

On Windows, the msvcrt module will be used.
On the Mac EasyDialogs.AskPassword is used, if available.

"""

# Authors: Piers Lauder (original)
# Guido van Rossum (Windows support and cleanup)

import sys

__all__ = ["getpass","getuser"]

def unix_getpass(prompt='Password: '):
    """Prompt for a password, with echo turned off.

    Restore terminal settings at end.
    """

    try:
        fd = sys.stdin.fileno()
    except:
        return default_getpass(prompt)

    old = termios.tcgetattr(fd) # a copy to save
    new = old[:]

    new[3] = new[3] & ~termios.ECHO # 3 == 'lflags'
    try:
        termios.tcsetattr(fd, termios.TCSADRAIN, new)
        passwd = _raw_input(prompt)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old)

    sys.stdout.write('\n')
    return passwd


def win_getpass(prompt='Password: '):
    """Prompt for password with echo off, using Windows getch()."""
    if sys.stdin is not sys.__stdin__:
        return default_getpass(prompt)
    import msvcrt
    for c in prompt:
        msvcrt.putch(c)
    pw = ""
    while 1:
        c = msvcrt.getch()
        if c == '\r' or c == '\n':
            break
        if c == '\003':
            raise KeyboardInterrupt
        if c == '\b':
            pw = pw[:-1]
        else:
            pw = pw + c
    msvcrt.putch('\r')
    msvcrt.putch('\n')
    return pw


def default_getpass(prompt='Password: '):
    print ("Warning: Problem with getpass. Passwords may be echoed.")
    return _raw_input(prompt)


def _raw_input(prompt=""):
    # A raw_input() replacement that doesn't save the string in the
    # GNU readline history.
    prompt = str(prompt)
    if prompt:
        sys.stdout.write(prompt)
    line = sys.stdin.readline()
    if not line:
        raise EOFError
    if line[-1] == '\n':
        line = line[:-1]
    return line


def getuser():
    """Get the username from the environment or password database.

    First try various environment variables, then the password
    database. This works on Windows as long as USERNAME is set.

    """

    import os

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

    # If this fails, the exception will "explain" why
    import pwd
    return pwd.getpwuid(os.getuid())[0]

# Bind the name getpass to the appropriate function
try:
    import termios
except ImportError:
    try:
        import msvcrt
    except ImportError:
        try:
            from EasyDialogs import AskPassword
        except ImportError:
            getpass = default_getpass
        else:
            getpass = AskPassword
    else:
        getpass = win_getpass
else:
    getpass = unix_getpass



</pre>

<script language="javascript" type="text/javascript">
    // You can place JavaScript like this

</script>
<?php
    // You can place PHP like this

?>
{/source}