Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`getpass` --- Portable password input |
| 2 | ========================================== |
| 3 | |
| 4 | .. module:: getpass |
| 5 | :synopsis: Portable reading of passwords and retrieval of the userid. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | .. moduleauthor:: Piers Lauder <piers@cs.su.oz.au> |
| 8 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 9 | .. Windows (& Mac?) support by Guido van Rossum. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 11 | **Source code:** :source:`Lib/getpass.py` |
| 12 | |
| 13 | -------------- |
| 14 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 | The :mod:`getpass` module provides two functions: |
| 16 | |
| 17 | |
Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 18 | .. function:: getpass(prompt='Password: ', stream=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 19 | |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 20 | Prompt the user for a password without echoing. The user is prompted using |
R David Murray | 604453c | 2014-04-14 10:28:58 -0400 | [diff] [blame] | 21 | the string *prompt*, which defaults to ``'Password: '``. On Unix, the |
| 22 | prompt is written to the file-like object *stream* using the replace error |
| 23 | handler if needed. *stream* defaults to the controlling terminal |
| 24 | (:file:`/dev/tty`) or if that is unavailable to ``sys.stderr`` (this |
| 25 | argument is ignored on Windows). |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 26 | |
| 27 | If echo free input is unavailable getpass() falls back to printing |
| 28 | a warning message to *stream* and reading from ``sys.stdin`` and |
| 29 | issuing a :exc:`GetPassWarning`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 31 | .. note:: |
| 32 | If you call getpass from within IDLE, the input may be done in the |
| 33 | terminal you launched IDLE from rather than the idle window itself. |
| 34 | |
| 35 | .. exception:: GetPassWarning |
| 36 | |
| 37 | A :exc:`UserWarning` subclass issued when password input may be echoed. |
| 38 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | |
| 40 | .. function:: getuser() |
| 41 | |
Berker Peksag | 1b207c5 | 2016-06-01 18:26:18 -0700 | [diff] [blame] | 42 | Return the "login name" of the user. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 | |
| 44 | This function checks the environment variables :envvar:`LOGNAME`, |
Barry Warsaw | d499031 | 2018-01-24 12:51:29 -0500 | [diff] [blame] | 45 | :envvar:`USER`, :envvar:`LNAME` and :envvar:`USERNAME`, in order, and |
| 46 | returns the value of the first one which is set to a non-empty string. If |
| 47 | none are set, the login name from the password database is returned on |
| 48 | systems which support the :mod:`pwd` module, otherwise, an exception is |
| 49 | raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 50 | |
Barry Warsaw | d499031 | 2018-01-24 12:51:29 -0500 | [diff] [blame] | 51 | In general, this function should be preferred over :func:`os.getlogin()`. |