一个极其简单的HTTP基本认证实现。
项目描述
python-basicauth
一个极其简单的HTTP基本认证编解码器。为什么?因为HTTP应该是极其简单的...这就是原因。
安装
要安装python-basicauth
,只需运行pip install basicauth
,就会自动安装最新版本。
用法
生成编码的基本认证字符串
>>> from basicauth import encode
>>> username, password = 'rdegges', 'omghax!!!'
>>> encoded_str = encode(username, password)
>>> print encoded_str
'Basic cmRlZ2dlczpvbWdoYXglMjElMjElMjE='
解码编码的基本认证字符串
>>> from basicauth import decode
>>> encoded_str = 'Basic cmRlZ2dlczpvbWdoYXglMjElMjElMjE=' # From the example above.
>>> username, password = decode(encoded_str)
>>> print (username, password)
('rdegges', 'omghax!!!')
我们也可以直接解码编码的字符串
>>> from basicauth import decode
>>> encoded_str = 'cmRlZ2dlczpvbWdoYXglMjElMjElMjE=' # From the example above.
>>> username, password = decode(encoded_str)
>>> print (username, password)
('rdegges', 'omghax!!!')
如果出现错误
>>> from basicauth import decode, DecodeError
>>> encoded_str = 'lol omg cmRlZ2dlczpvbWdoYXglMjElMjElMjE=' # Invalid encoded string.
>>> username, password = decode(encoded_str)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "basicauth/__init__.py", line 49, in decode
raise DecodeError
basicauth.DecodeError
注意:HTTP基本认证规范(RFC 2617)不允许在用户名字段中包含任何冒号字符(:
)。密码字段中允许使用冒号 -- 就这些。
测试
想要运行测试?没问题
$ git clone https://github.com/rdegges/python-basicauth.git
$ cd python-basicauth
$ pip install -e .
...
$ python -m unittest
..............
----------------------------------------------------------------------
Ran 14 tests in 0.103s
OK
项目详情
下载文件
下载您平台对应的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。
源代码分发
basicauth-1.0.0.tar.gz (3.6 kB 查看哈希值)
构建分发
basicauth-1.0.0-py3-none-any.whl (3.8 kB 查看哈希值)