# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import socket
import ssl

HOST = 'localhost'
PORT = 12345
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
#ctx.options |= ssl.PROTOCOL_TLSv1_2
#ctx.options |= ssl.OP_NO_SSLv2
#ctx.options |= ssl.OP_NO_SSLv3
#ctx.options |= ssl.OP_NO_TLSv1
#ctx.options |= ssl.OP_NO_TLSv1_1
#ctx.options |= ssl.OP_NO_TLSv1_3
sraw = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sraw.connect((HOST, PORT))
s = ctx.wrap_socket(sraw, server_hostname=HOST)
print('connected')
print('server is %s' % (s.getpeercert())) # empty with CERT_NONE - should work with CERT_REQUIRED, but that does not work with self signed
wf = s.makefile('w')
rf = s.makefile('r')
a = 123
b = 456
wf.write('%d %d\n' % (a, b))
wf.flush()
line = rf.readline().rstrip()
c = int(line)
print('%d' % (c))
v = 'ABC'
wf.write('%s\n' % (v))
wf.flush()
v2 = rf.readline().rstrip()
print(v2)
s.close()
