Python 隱藏輸入密碼時屏幕回顯的實例
我們再登錄賬號密碼的時候,賬號可以回顯在屏幕上,但是對于比較隱私的項目例如密碼最好是不要再屏幕上回顯。就像我們再終端登錄linux服務(wù)器的時候,輸入信息的時候只顯示用戶名,而不顯示登錄密碼。
以下是Python代碼對比:
1、明文回顯賬號密碼
##代碼
[root@room1pc01 data]# cat login1.py
#!/usr/bin/env python2.6
username = raw_input('username:')
password = raw_input('password:')
if username == 'bob':
if password == '123456':
print 'Login success.'
else:
print 'Your password error.'
else:
print 'Your username error.'
##運行效果
[root@room1pc01 data]# python login1.py
username:bob
password:123456 ##密碼明文回顯在屏幕上,不安全
Login success.
2、使用getpass.getpass不回顯輸入的隱私信息
##代碼
[root@room1pc01 data]# cat login2.py
#!/usr/bin/env python2.6
import getpass
username = raw_input('username:')
password = getpass.getpass('password:')
if username == 'bob':
if password == '123456':
print 'Login success.'
else:
print 'Your password error.'
else:
print 'Your username error.'
##運行效果
[root@room1pc01 data]# python login2.py
username:bob
password: ##密碼沒有在屏幕上回顯,私密信息得到保障
Login success.
以上這篇Python 隱藏輸入密碼時屏幕回顯的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python編程使用*解包和itertools.product()求笛卡爾積的方法
這篇文章主要介紹了Python編程使用*解包和itertools.product()求笛卡爾積的方法,涉及Python列表轉(zhuǎn)換及itertools.product()求笛卡爾積相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
python數(shù)據(jù)分析基礎(chǔ)知識之shape()函數(shù)的使用教程
shape函數(shù)是numpy.core.fromnumeric中的函數(shù),它的功能是讀取矩陣的長度,比如shape[0]就是讀取矩陣第一維度的長度,下面這篇文章主要給大家介紹了關(guān)于python數(shù)據(jù)分析基礎(chǔ)知識之shape()函數(shù)使用的相關(guān)資料,需要的朋友可以參考下2022-09-09
Python網(wǎng)絡(luò)編程使用select實現(xiàn)socket全雙工異步通信功能示例
這篇文章主要介紹了Python網(wǎng)絡(luò)編程使用select實現(xiàn)socket全雙工異步通信功能,簡單說明了select模塊的功能及socket全雙工異步通信功能的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-04-04

