如何明确告诉 Python 的 ldap3 模块为 TLS 1.2 版?

How to tell Python's ldap3 module to TLS version 1.2 explicitly?

我正在尝试使用 ldap3 Python 模块对 ldap 进行身份验证,但我想验证我的连接是否使用 TLS 版本 1.2。

相关代码片段:

tls = Tls(validate=ssl.CERT_REQUIRED, version=ssl.PROTOCOL_TLSv1_2)
server = Server(server_uri, use_ssl=True, tls=tls, get_info=ALL)
conn = Connection(server, user="domain\myusername", password="password", authentication=NTLM, auto_referrals=False)
conn.bind()

我的连接对象的输出:

Connection(server=Server(
host='domain.host.com', 
port=636, 
use_ssl=True, 
allowed_referral_hosts=[('*', True)], 
tls=Tls(validate=<VerifyMode.CERT_REQUIRED: 2>, 
version=<_SSLMethod.PROTOCOL_TLSv1_2: 5>), 
get_info='ALL', mode='IP_V6_PREFERRED'), 
version=3, 
authentication='NTLM', 
client_strategy='SYNC', ...truncated...)

我不确定版本 属性 中的“5”是什么意思,但最终我会尝试验证我是否使用 TLS 1.2 版进行身份验证。

常量来自 ssl stdlib 模块。您可以使用该模块为不同的 SSL 版本打印常量等价物,如下所示:

$ python -c "import ssl; print(ssl.PROTOCOL_SSLv3)"
1
$ python -c "import ssl; print(ssl.PROTOCOL_SSLv23)"
2
$ python -c "import ssl; print(ssl.PROTOCOL_TLSv1)"
3
$ python -c "import ssl; print(ssl.PROTOCOL_TLSv1_1)"     
4
$ python -c "import ssl; print(ssl.PROTOCOL_TLSv1_2)"
5