歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python 之getpass模塊

Python 之getpass模塊

日期:2017/3/1 9:46:06   编辑:Linux編程

Python 之getpass模塊

getpass.getpass([prompt[, stream]]):

prompt:為用戶輸入的提示字符串,默認為:Password:

>>> pwd = getpass.getpass()

Password:

>>> print pwd

>>> pwd = getpass.getpass(prompt='please input your password: ')

please input your password:

>>> print pwd

dudiaohanjiangxue


getpass模塊提供了兩個函數。


getpass.getpass([prompt[, stream]]):提示用戶輸入密碼。用戶輸入的內容並不會在屏幕上顯示出來。

###########

getpass.getuser():獲得登陸的用戶名

>>> user = getpass.getuser()

>>> print user

root

#This function checks the environment variables LOGNAME, USER, LNAME and USERNAME, in order, and returns the value of the first one which is set to a non-empty string. If none are set, the login name from the password database is returned on systems which support the pwd module, otherwise, an exception is raised

#這個函數會按順序檢查環境變量:LOGNAME,USER,LNAME,USERNAME

>>> import os

>>> os.system('echo $LOGNAME')

root

0

>>> os.system('echo $USER')

root

0

>>> os.system('echo $LNAME')

0

Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved