getpass — 이식성 있는 암호 입력¶
소스 코드: Lib/getpass.py
The getpass module provides two functions:
- getpass.getpass(prompt='Password: ', stream=None, *, echo_char=None)¶
Prompt the user for a password without echoing. The user is prompted using the string prompt, which defaults to
'Password: '. On Unix, the prompt is written to the file-like object stream using the replace error handler if needed. stream defaults to the controlling terminal (/dev/tty) or if that is unavailable tosys.stderr(this argument is ignored on Windows).The echo_char argument controls how user input is displayed while typing. If echo_char is
None(default), input remains hidden. Otherwise, echo_char must be a single printable ASCII character and each typed character is replaced by it. For example,echo_char='*'will display asterisks instead of the actual input.If echo-free input is unavailable,
getpass()falls back to printing a warning message to stream and reading fromsys.stdinand issuing aGetPassWarning.참고
If you call
getpass()from within IDLE, the input may be done in the terminal you launched IDLE from rather than the idle window itself.참고
On Unix systems, when echo_char is set, the terminal will be configured to operate in noncanonical mode. Common terminal control characters are supported:
Ctrl+A - Move cursor to beginning of line
Ctrl+E - Move cursor to end of line
Ctrl+K - Kill (delete) from cursor to end of line
Ctrl+U - Kill (delete) entire line
Ctrl+W - Erase previous word
Ctrl+V - Insert next character literally (quote)
Backspace/DEL - Delete character before cursor
These shortcuts work by reading the terminal’s configured control character mappings from termios settings.
버전 3.14에서 변경: Added the echo_char parameter for keyboard feedback.
버전 3.15에서 변경: When using non-empty echo_char on Unix, keyboard shortcuts (including cursor movement and line editing) are now properly handled using the terminal’s control character configuration.
- exception getpass.GetPassWarning¶
패스워드 입력이 에코 될 때 방출되는
UserWarning서브 클래스.
- getpass.getuser()¶
사용자의 “로그인 이름”을 반환합니다.
이 함수는 환경 변수
LOGNAME,USER,LNAME및USERNAME를 순서대로 검사하고, 비어 있지 않은 문자열로 설정된 첫 번째 값을 반환합니다. 아무것도 설정되지 않았으면,pwd모듈을 지원하는 시스템에서는 암호 데이터베이스의 로그인 이름이 반환되고, 그렇지 않으면OSError가 발생합니다.일반적으로, 이 함수는
os.getlogin()보다 선호됩니다.버전 3.13에서 변경: Previously, various exceptions beyond just
OSErrorwere raised.