用过Linux的都知道,尤其是进行使用包管理软件类似于apt-get这种的,密码是不回显的。在一定程度上可以保护我们的密码不被旁边的人看到,保护个人的隐私。但是很多时候,我们自己也是深受其害,因为不知道自己已经输入了几个字符了,这就有可能会让不自信的人删完,再重输入一遍。

同样的Python标准库里面有一个叫getpass的库,比较简单,针对于windows 和 Mac OS X都适配。用途也很广泛,但是唯一的缺点可能就跟上面说道的一样,不能显示已经输入了多少个字符! 这就显得很尴尬了。

那么今天就从易用性的角度出发,写一个更好用的getpass出来吧。


编程伊始

类比于一个进度条,在输入数据的时候,是不能够换行的。也就是说,提示语句只会在本行内显示。

常用的print函数末尾会自动的换行回车,所以我们不能用它了。然而sys.stdout.write则不会在后面添加额外的东西。二者正是我们想要的。但是其存在一个问题,那就是不强制输出的话,默认不输出信息,所以我们还需要强制输出一下。借助于sys.stdout.flush函数即可。

下面先来看个小例子,就很容易明白了。

def test():
    import sys, time

    for i in range(5):
        sys.stdout.write(str(i) * (5 - i) + '\r')
        sys.stdout.flush()
        time.sleep(1)

运行结果如下:

正式实施

根据上面的思想,稍微动动脑筋我们就可以实现想要的功能了。代码很简单,大致如下:

# coding:utf-8
import sys, os
from time import *

reload(sys)
sys.setdefaultencoding('utf8')
#    __author__ = '郭 璞'
#    __date__ = '2016/10/15'
#    __Desc__ = 改进版的getpass,更易用,更好用!

def getpass(prompt='', tip = 'You have typed %d characters!'):
    # print the prompt to prompt user what to do
    print prompt
    import msvcrt
    # get one charecter from terminal
    cur_character = msvcrt.getch()
    # the result will be out final answer!
    result =''+cur_character
    count = 1
    # 循环的读取从控制台输入的字符数据 get character once a time from terminal
    while cur_character!= '\r':
        # show message at the old line with the help of  the function named sys.stdout.write and sys.stdout.flush
        sys.stdout.write(tip%(count)+"\r")
        sys.stdout.flush()
        # update the needed item
        cur_character = msvcrt.getch()
        result += cur_character
        count += 1
    # to avoid overlap the message, we'd better go to next new line.
    print "\n"
    # return what we really need
    return result

if __name__ == '__main__':
    password = getpass(prompt='\bplease input your username(there should contains no space):')
    print password

运行结果,符合我们的需求,如下:

最后输出了输入结果的原因就是我们main函数里面故意输出了,具体怎么用取决于您的代码。

改进版

至于改进版,是添加了自定义delimiter需求,以及完善“中途删除操作”等。

源码

# coding:utf-8
import sys, os
from time import *

reload(sys)
sys.setdefaultencoding('utf8')
#    __author__ = '郭 璞'
#    __date__ = '2016/10/15'
#    __Desc__ =   just like getpass lib, this is also simple and easy to use. While the drawback is it's not so much pythonic in the implement of the function 'replace'.
#                  I am happy to hear you can join me to slove it. :)
#                 改进版的getpass,更易用,更好用! 唯一的缺点就是replace函数中的实现略显粗糙,欢迎前来完善。

# show the numbers we have typed
def showByNumbers(prompt='', tip = 'You have typed %d characters!'):
    # print the prompt to prompt user what to do
    print prompt
    import msvcrt
    # get one charecter from terminal
    cur_character = msvcrt.getch()
    # the result will be out final answer!
    result =''+cur_character
    count = 1
    # 循环的读取从控制台输入的字符数据 get character once a time from terminal
    while cur_character!= '\r':
        # show message at the old line with the help of  the function named sys.stdout.write and sys.stdout.flush
        sys.stdout.write(tip%(count)+"\r")
        sys.stdout.flush()
        # update the needed item
        cur_character = msvcrt.getch()
        if cur_character =="\b":
            result = result[0:-1]
            count -=1
        else:
            result += cur_character
            count += 1
    # to avoid overlap the message, we'd better go to next new line.
    print "\n"
    # return what we really need
    return result

# use the constom char to replace what we typed
def replaceWithDelimiter(prompt='', delimiter = '*'):
    print prompt
    import msvcrt

    current_character = msvcrt.getch()
    count = 1
    result = ''
    result += current_character

    # if we typed backspace key, we should update the interface right now!
    delta = 0

    # get a character once a time and do something if meet the backspace
    while current_character!='\r':
        # build the stars to show
        stars = ''
        for index in range(count):
            stars += delimiter
            for i in range(delta):
                stars += ' '
        sys.stdout.write(prompt + stars + "\r")
        sys.stdout.flush()

        # update for next loop
        current_character = msvcrt.getch()
        # consider the backspace key
        if current_character == '\b':
            count -=1
            result = result[0:-1]
            # for erase the extra delimiters
            delta +=1
        else:
            result += current_character
            count +=1

    # it's necessary to print in a new line for avoiding overlapping
    print "\n"
    # return what we really need
    return result

def getpass(prompt='', mode='n', tip='You have typed %d characters!', delimiter='*'):

    if mode =='n':
        return showByNumbers(prompt=prompt, tip=tip)
    elif mode == 'r':
        return replaceWithDelimiter(prompt=prompt, delimiter=delimiter)
    else:
        raise NameError('There are only two choice, so check your input!')

if __name__ == '__main__':
    """
    Here are some useful test. And you can also do it just like this!
    :)
    """
    # password = getpass(prompt='\bplease input your username(there should contains no space):')
    # print password
    # password = replaceWithDelimiter(prompt='\bplease input your username(there should contains no space):', delimiter='')
    # print password
    password = getpass(prompt='\bplease input your username(there should contains no space):', mode='n')
    print password

    pwd = getpass(prompt='\bplease input your username(there should contains no space):', mode='r', delimiter='#')
    print pwd

以数字显示

以自定义分隔符delimiter显示

如何使用?

由于博主将这个工具封装好了,而且已经上传到了pypi上面。所以大家可以很容易的下载以及安装。

下载及安装

pip install getpass2

在您的代码中使用

不出意外的话,使用下面的代码即可。

from getpass2 import getpass

......

如果,没能正常的工作,找到您的Python的site-packages,找到getpass2文件夹,将里面的getpass2.py拷贝到您的项目中即可。同样可以完美的运行。

源码下载

如果您想直接看一下源码的话,可以到博主的GitHub上直接下载。下载链接如下:

https://github.com/guoruibiao/getpass2

总结

本篇文章主要是介绍了完善Python标准库的一个小案例。其实并没有什么难于理解的地方。关键在于动手实践。

如果您对此有兴趣,不妨fork 或者star来一起完善一下吧。

(^__^) 嘻嘻……

改进版getpass库的更多相关文章

  1. 《Ansible权威指南》笔记(3)——Ad-Hoc命令集,常用模块

    五.Ad-Hoc命令集1.Ad-Hoc命令集通过/usr/bin/ansible命令实现:ansible <host-pattern> [options]    -v,--verbose  ...

  2. python学习笔记——(二)循环

    ·密文输入引入getpass库使用getpass.getpass("password:")tips:该功能在PyCharm中不好使,只能在命令行用 ·python强制缩进,省略了大 ...

  3. Python学习【02】Python基础

    一.Python内部执行过程 1.Python的后缀名可以是任意? print("hello,world")  保存成  .py / .txt / .sb / .aaa 都可以用在 ...

  4. 3rd,Python登录模拟

    需求: 1. 用户输入帐号密码进行登陆 2. 用户信息保存在文件内 3. 用户密码输入错误三次后锁定用户 #导入getpass库,用于密码验证 import getpass #定义用户名.密码 _us ...

  5. Python在七牛云平台的应用(一)

    七牛云:(引用百度的介绍)七牛云是国内领先的企业级公有云服务商,致力于打造以数据为核心的场景化PaaS服务.围绕富媒体场景,七牛先后推出了对象存储,融合CDN加速,数据通用处理,内容反垃圾服务,以及直 ...

  6. Python趣味实用小工具

    代码地址如下:http://www.demodashi.com/demo/12918.html python 趣味实用小工具 概述 用python实现的三个趣味实用小工具: 图片转Execl工具 , ...

  7. 4.1、Ansible模块

    ansible-doc -l    列出所有模块 ansible-doc 模块名    查看模块的help说明 ansible-doc -s  module_name:获取指定模块的使用信息 ***文 ...

  8. python3学习笔记一

    install 安装软件包download 下载安装包uninstall 卸载安装包freeze 按照req uirements 格式输出安装包,可以到其他服务器上执行pip install -r r ...

  9. DAY2 Python 标准库 -> Getpass 模块 -> 命令行下输入密码的方法.

    getpass 模块 getpass 模块提供了平台无关的在命令行下输入密码的方法. getpass(prompt) 会显示提示字符串, 关闭键盘的屏幕反馈, 然后读取密码. 如果提示参数省略, 那么 ...

随机推荐

  1. C#之Excel操作

    下面的这几个方法是我在项目中经常用到的,欢迎大家批评指正 读取Excel表中的数据 第一种:功能丰富,速度慢 /// <summary> /// 从Excel读取数据 /// </s ...

  2. [LeetCode] Find Duplicate File in System 在系统中寻找重复文件

    Given a list of directory info including directory path, and all the files with contents in this dir ...

  3. Linux服务器断电导致挂载及xfs文件损坏的修复方法

    系统文件损坏后进入紧急修复模式,无法进行维护工作 welcome to emergency mode!after logging in ,type "journalctl -xb" ...

  4. 用DotTrace 来分析.NET-Core程序

    1. 前言   看园子里面讲dotTrace 的文章不多,最近也有这方面的需要,于是去搜索了一下,.NET 性能分析方面的工具.目的呢,主要是想发现我的代码中,哪些代码占用了最多时间,来进行优化.主要 ...

  5. [Codeforces 922E]Birds

    Description 题库链接 一条直线上有 \(n\) 棵树,每棵树上有 \(c_i\) 只鸟,在一棵树底下召唤一只鸟的魔法代价是 \(cost_i\) 每召唤一只鸟,魔法上限会增加 \(B\) ...

  6. [HNOI 2017]礼物

    Description 我的室友最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决定买一对情侣手 环,一个留给自己,一个送给她.每个手环上各有 n 个装饰物,并且每个装饰物都有一定的亮度.但是在 ...

  7. [Educational Codeforces Round#22]

    来自FallDream的博客,未经允许,请勿转载,谢谢. 晚上去clj博客逛来逛去很开心,突然同学提醒了一下,发现cf已经开始40分钟了,慌的一B,从B题开始写,写完了B到E最后收掉了A,结果太着急B ...

  8. Thinkphp中的 I 函数(Thinkphp3.2.3版本)

    I 函数的作用是获取系统变量,必要时还可以对变量值进行过滤及强制转化,I 函数的语法格式: I('变量类型.变量名/修饰符',['默认值'],['过滤方法或正则'],['额外数据源']) 一.获取变量 ...

  9. Linux之grep命令

    概述 所有的类linux系统都会提供一个名为grep(global regular expression print,全局正则表达式输出)的搜索工具.grep命令在对一个或多个文件的内容进行基于模式的 ...

  10. VS2012不能加载想要打开的项目/解决方案

    今天回宿舍用自己的电脑敲代码,想要打开之前的项目,可是VS2012打开之后项目却显示“无法加载” 查了之后才知道原来是由于某个安装包缺少引起的,具体做法请看如下 链接:http://jingyan.b ...