改进版getpass库

简介: 编程伊始正式实施改进版源码以数字显示以自定义分隔符delimiter显示如何使用下载及安装在您的代码中使用源码下载总结用过Linux的都知道,尤其是进行使用包管理软件类似于apt-get这种的,密码是不回显的。

用过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

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

最后输出了输入结果的原因就是我们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

以数字显示

带删除操作的getpass

以自定义分隔符delimiter显示

自定义分隔符的getpass,支持删除操作

如何使用?

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

getpass2 in pypi

下载及安装

pip install getpass2

在您的代码中使用

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

from getpass2 import getpass

......

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

源码下载

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

https://github.com/guoruibiao/getpass2

源码下载

总结

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

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

(^__^) 嘻嘻……

目录
相关文章
|
3月前
|
Python
技术经验解读:【Python】torrentParser1.04增加获得磁力链URI功能
技术经验解读:【Python】torrentParser1.04增加获得磁力链URI功能
21 0
|
4月前
|
存储 搜索推荐 C++
【C++高阶(二)】熟悉STL中的map和set --了解KV模型和pair结构
【C++高阶(二)】熟悉STL中的map和set --了解KV模型和pair结构
|
4月前
|
存储 数据采集 搜索推荐
BoostCompass(数据准备预处理模块)
BoostCompass(数据准备预处理模块)
43 0
|
11月前
|
大数据 测试技术
关于 ABAP 函数调用 Pass by value 和 Pass by reference 的性能比较
关于 ABAP 函数调用 Pass by value 和 Pass by reference 的性能比较
|
机器学习/深度学习 PyTorch 算法框架/工具
PyTorch并行与分布式(三)DataParallel原理、源码解析、举例实战
PyTorch并行与分布式(三)DataParallel原理、源码解析、举例实战
692 0
|
SQL 存储 JSON
Spark学习---5、SparkSQL(概述、编程、数据的加载和保存)(二)
Spark学习---5、SparkSQL(概述、编程、数据的加载和保存)(二)
|
SQL 缓存 分布式计算
Spark学习---5、SparkSQL(概述、编程、数据的加载和保存)(一)
Spark学习---5、SparkSQL(概述、编程、数据的加载和保存)(一)
|
缓存 Linux Windows
certutil工具使用和bypass学习
certutil工具使用和bypass学习
706 0
|
Linux C语言 Python
Linux对稀疏(Sparse)文件的支持
稀疏(Sparse)文件的创建 在EXT2/EXT3文件系统上可以使用dd创建稀疏文件: $ dd if=/dev/zero of=fs.img bs=1M seek=1024 count=00+0 records in0+0 records out$ ls -lh fs.
1181 0
|
机器学习/深度学习 人工智能 算法
图解python | pass语句
在Python中,有时候程序需要占一个位置或放一条语句,但又不希望这条语句做任何事情,此时就可以通过 pass 来优雅地实现。
113 0
图解python | pass语句