From cd475317dfed40af946ea89a6df444f3fd03d4ea Mon Sep 17 00:00:00 2001 From: Nemunaire Date: Sun, 1 Feb 2015 18:09:19 +0100 Subject: [PATCH] Perform protocol tests --- eyespot/protocols.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 eyespot/protocols.py diff --git a/eyespot/protocols.py b/eyespot/protocols.py new file mode 100644 index 0000000..4785241 --- /dev/null +++ b/eyespot/protocols.py @@ -0,0 +1,40 @@ +import socket +import ssl + + +def get(): + """Retrive a list of supported protocols by this Python version""" + + protos = [] + for p in ["PROTOCOL_SSLv2", + "PROTOCOL_SSLv3", + "PROTOCOL_TLSv1", + "PROTOCOL_TLSv1_1", + "PROTOCOL_TLSv1_2"]: + if hasattr(ssl, p): + protos.append(getattr(ssl, p)) + + return protos + + +def test(host, protocol): + """Test if a service's host against the given protocol + + Arguments: + host -- tuple (hostname, port) to test + protocol -- protocol to test + """ + + context = ssl.SSLContext(protocol) + context.set_ciphers("ALL") + + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + ssl_sock = context.wrap_socket(s) + try: + ssl_sock.connect(host) + return True + except ssl.SSLError: + pass + except ConnectionResetError: + pass + return False