RADIUS认证模块
项目描述
py-radius
Python 2.7.13+的RADIUS认证模块
(c) 1999 Stuart Bishop <zen@shangri-la.dropbear.id.au>
此模块提供了基本的RADIUS客户端功能,允许您的Python代码对任何符合RFC2138规范的RADIUS服务器进行认证。
安装
$ pip install py-radius
使用方法
radius.py模块可以从命令行运行,提供一个最小的RADIUS客户端来测试RADIUS服务器
$ python -m radius Host [default: 'radius']: radius Port [default: 1812]: 1812 Enter RADIUS Secret: s3cr3t Enter your username: foobar Enter your password: qux ... Authentication Successful
示例
以下是使用库的一个示例。
import radius
radius.authenticate(username, password, secret, host='radius', port=1812)
# - OR -
r = radius.Radius(secret, host='radius', port=1812)
print('success' if r.authenticate(username, password) else 'failure')
如果您的RADIUS服务器需要挑战/响应,使用方法会更复杂一些。
import radius
r = radius.Radius(secret, host='radius')
try:
print('success' if r.authenticate(username, password) else 'failure')
sys.exit(0)
except radius.ChallengeResponse as e:
pass
# The ChallengeResponse exception has `messages` and `state` attributes
# `messages` can be displayed to the user to prompt them for their
# challenge response. `state` must be echoed back as a RADIUS attribute.
# Send state as an attribute _IF_ provided.
attrs = {'State': e.state} if e.state else {}
# Finally authenticate again using the challenge response from the user
# in place of the password.
print('success' if r.authenticate(username, response, attributes=attrs)
else 'failure')
此模块具有详细的日志记录,您可以使用Python日志框架启用它。