python 密码输入显示星号_[145]python实现控制台密码星号输入

import msvcrt, sys, os

print('password: ', end='', flush=True)

li = []

while 1:

ch = msvcrt.getch()

#回车

if ch == b'\r':

msvcrt.putch(b'\n')

print('输入的密码是:%s' % b''.join(li).decode())

break

#退格

elif ch == b'\x08':

if li:

li.pop()

msvcrt.putch(b'\b')

msvcrt.putch(b' ')

msvcrt.putch(b'\b')

#Esc

elif ch == b'\x1b':

break

else:

li.append(ch)

msvcrt.putch(b'*')

os.system('pause')

示例

5b9afaf1aca20e542e0b30f6a5c2612d.gif

一、raw_input()或input():

for python 2.x

[root@master test]# /usr/local/python2.7/bin/python test.py

Please input your password:123

your password is 123

[root@master test]# cat test.py

#!/usr/bin/python

# -*- coding=utf-8 -*-

#for python 2.x

#input = raw_input("Please input your password:")

#print "your password is %s" %input

for python 3.x

[root@master test]# /usr/local/python3.4/bin/python3 test.py

Please input your password:123

your password is 123

[root@master test]# cat test.py

#!/usr/bin/python

# -*- coding=utf-8 -*-

#for python 3.x

input = input("Please input your password:")

print ("your password is %s" %input)

Note:这种方法最简单,但是不安全,很容易暴露密码。

二、getpass.getpass():

for python 2.x

[root@master test]# /usr/local/python2.7/bin/python test.py

Please input your password:

your password is 123

[root@master tes

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值