-
-
Notifications
You must be signed in to change notification settings - Fork 641
/
features.py
40 lines (33 loc) · 1.34 KB
/
features.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""Introspect Python's SSL library and list its features and options.
This script was not included in the text of the book itself, but was a
useful enough tool to me while I was writing the book that I thought I
should leave it sitting around in the source code repository.
- Brandon Rhodes, for Foundations of Python Network Programming,
Third Edition
"""
try:
import ssl
except ImportError:
ssl = None
def main():
if ssl is None:
print('This Python is not compiled with SSL support')
return
names = dir(ssl)
print()
display(names, ' protocol ', lambda s: s.startswith('PROTOCOL_'))
display(names, ' verify_mode ', lambda s: s.startswith('CERT_'))
display(names, ' verify_flags ', lambda s: s.startswith('VERIFY_'))
display(names, ' options ', lambda s: s.startswith('OP_'))
display(names, ' feature availability ', lambda s: s.startswith('HAS_'))
def display(names, title, test):
items = [(fix(getattr(ssl, name)), name) for name in names if test(name)]
print(title.center(72, '-'))
for value, name in sorted(items):
print('{:27} {:10} {:>32}'.format(name, value, bin(value)[2:]))
print()
def fix(value):
"""Turn negative 32-bit numbers into positive numbers."""
return (value + 2 ** 32) if (value < 0) else value
if __name__ == '__main__':
main()