from functools import wraps
from ssl import SSLContext, create_default_context
def wrapper(set_alpn_protocols):
"""
Wrapper designed for SSLContext.set_alpn_protocols
"""
@wraps(set_alpn_protocols)
def new_setter(self, alpn_protocols):
set_alpn_protocols(self, alpn_protocols) # This has no return value
self._alpn_protocols = alpn_protocols # You can change the attr name if you want
return new_setter
SSLContext.set_alpn_protocols = wrapper(SSLContext.set_alpn_protocols)
context = create_default_context()
context.set_alpn_protocols(['h2', 'http/1.1'])
# Now if the setter was ever used, you can access it through _alpn_protocols attribute
try:
print(context._alpn_protocols)
except AttributeError:
print('setter was not called')
1条答案
按热度按时间euoag5mw1#
getter没有在python API中公开,这是一个经常出现的主题,通常使用python对OpenSSL进行 Package 。为此,您必须对
SSLContext.set_alpn_protocols
方法进行monkey-patch,以便将提供的alpn协议存储在示例属性(或其他地方)中。这样,您就可以从应用程序的其他部分访问它。示例:输出