1#!/usr/local/bin/python3.8
2
3"""Synchronous client using python-gnutls"""
4
5import sys
6import os
7import socket
8
9from gnutls.crypto import *
10from gnutls.connection import *
11from gnutls.errors import GNUTLSError
12
13script_path = os.path.realpath(os.path.dirname(sys.argv[0]))
14certs_path = os.path.join(script_path, 'certs')
15
16cert = X509Certificate(open(certs_path + '/valid.crt').read())
17key = X509PrivateKey(open(certs_path + '/valid.key').read())
18ca = X509Certificate(open(certs_path + '/ca.pem').read())
19crl = X509CRL(open(certs_path + '/crl.pem').read())
20cred = X509Credentials(cert, key, [ca], [crl])
21context = TLSContext(cred)
22
23sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
24session = ClientSession(sock, context)
25
26try:
27    session.connect(('localhost', 10000))
28    session.handshake()
29    session.verify_peer()
30    session.send("test\r\n")
31    buf = session.recv(1024)
32    print 'Received: ', buf.rstrip()
33    session.bye()
34    session.close()
35except GNUTLSError as e:
36    print('Connection failed: {}'.format(e))
37    sys.exit(1)
38