Python中的getpass模块学习

getpass模块提供了平台无关的在命令行下输入密码的方法; 该模块主要提供:

两个函数: getuser, getpass
一个报警: GetPassWarning(当输入的密码可能会显示的时候抛出,该报警为UserWarning的一个子类)
1、getpass函数
from getpass import getpass
pwd=getpass()
Warning (from warnings module):
File “C:\Program Files (x86)\Python27\lib\getpass.py”, line 92
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
Password: 123456
备注: Warning: Password input may be echoed.
2、getuser函数
from getpass import getuser
usr=getuser()
print usr
Administrator
备注:该函数会检查环境变量LOGNAME,USER,LNAME 和USERNAME, 以返回一个非空字符串。如果这些变量的设置为空的话,会从支持密码的数据库中获取用户名,否则会触发一个找不到用户的异常!

3、getpass函数参数
getpass.getpass([prompt[, stream]])
增加提示语
getpass(“请输入密码–>”)

4、使用写法
from getpass import *
def check_user_pass(user,password):
#注意参数类型均为字符串
  if user==‘Administrator’ and password==‘123456’:
    print “success”
    return True
  else:
    reurn False
if name==main:
  usr=getuser()
  pwd=getpass(“请输入密码:”)
  if check_user_pass(usr,pwd):
    print “now check pass”
  else:
    print “wrong username or password”

你可能感兴趣的:(python)