+def get_openssl_version():
+ try:
+ from OpenSSL import SSL
+ return extract_openssl_version(SSL)
+ except Exception:
+ return ("unknown", None, None)
+
+def extract_openssl_version(ssl_module):
+ openssl_version = ssl_module.SSLeay_version(ssl_module.SSLEAY_VERSION)
+ if openssl_version.startswith('OpenSSL '):
+ openssl_version = openssl_version[8 :]
+
+ (version, _, comment) = openssl_version.partition(' ')
+
+ try:
+ openssl_cflags = ssl_module.SSLeay_version(ssl_module.SSLEAY_CFLAGS)
+ if '-DOPENSSL_NO_HEARTBEATS' in openssl_cflags.split(' '):
+ comment += ", no heartbeats"
+ except Exception:
+ pass
+
+ return (version, None, comment if comment else None)