getpass模块

来源:互联网 发布:推广必备软件 编辑:程序博客网 时间:2024/04/27 13:14

http://docs.python.org/2/library/getpass.html

The getpass module provides two functions:

getpass.getpass([prompt[stream]])

Prompt the user for a password without echoing.The user is prompted using the string prompt, which defaults to 'Password: '

http://pymotw.com/2/getpass/

Many programs that interact with the user via the terminal need to ask the user for password values without showing what the user types on the screen.The getpass module provides a portable way to handle such password prompts securely.

Example

The getpass() function prints a prompt then reads input from the user until they press return. The input is passed back as a string to the caller.

import getpassp = getpass.getpass()print 'You entered:', p

The default prompt, if none is specified by the caller, is “Password:”.

$ python getpass_defaults.pyPassword:You entered: sekret

The prompt can be changed to any value your program needs.

import getpassp = getpass.getpass(prompt='What is your favorite color? ')if p.lower() == 'blue':    print 'Right.  Off you go.'else:    print 'Auuuuugh!'

I don’t recommend such an insecure authentication scheme, but it illustrates the point.

$ python getpass_prompt.pyWhat is your favorite color?Right.  Off you go.$ python getpass_prompt.pyWhat is your favorite color?Auuuuugh!

原创粉丝点击