简单介绍:
此模块儿常用来提供平台无关的在命令行下输入密码的方法
常用方法:
getpass.getpass(prompt[, stream]) -> str
说明:获取密码,prompt是用户输入提示字符串,默认为Password:,用户输入的内容不会在屏幕上显示出来
getpass.getuser() -> str
说明:获取当前登录的用户名,此函数会按照顺序检查(${LOGNAME}, ${USER}, ${LNAME}, ${USERNAME})
# -*- coding: utf-8 -*-
"""
#
# Authors: limanman
# OsChina: http://my.oschina.net/pydevops/
# Purpose:
#
"""
import getpass
import colorama
def main():
"""Main function."""
with open(USER_FILE, 'w+b') as whandler:
user_info = {}.fromkeys(['user_name', 'user_pass'], None)
user_info['user_name'] = raw_input('please enter your name: ')
user_info['user_pass'] = getpass.getpass(prompt=('please enter your'
'pass: '))
print >>whandler, '%s:%s' % (user_info['user_name'],
user_info['user_pass'])
rhandler = file(USER_FILE, 'r+b')
notice_head_msg = 'User Infomations:'
print ''.join([colorama.Fore.YELLOW, notice_head_msg])
for cur_line in rhandler:
cur_line = cur_line.strip()
user_name = cur_line.split(':')[0]
user_pass = cur_line.split(':')[1]
print '%*s : %-s' % (len(notice_head_msg)-2, user_name, user_pass)
if not rhandler.closed:
rhandler.close()
if __name__ == '__main__':
USER_FILE = 'user_info.txt'
colorama.init(autoreset=True)
main()