summaryrefslogtreecommitdiff
path: root/examples/sni/client.py
blob: d4a751f756d6e5ef6a967ba0498806fd333b7c9e (plain)
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
# Copyright (C) Jean-Paul Calderone
# See LICENSE for details.

from __future__ import print_function

from sys import argv, stdout
from socket import socket

from OpenSSL.SSL import TLSv1_METHOD, Context, Connection


def main():
    """
    Connect to an SNI-enabled server and request a specific hostname, specified
    by argv[1], of it.
    """
    if len(argv) < 2:
        print('Usage: %s <hostname>' % (argv[0],))
        return 1

    client = socket()

    print('Connecting...', end="")
    stdout.flush()
    client.connect(('127.0.0.1', 8443))
    print('connected', client.getpeername())

    client_ssl = Connection(Context(TLSv1_METHOD), client)
    client_ssl.set_connect_state()
    client_ssl.set_tlsext_host_name(argv[1])
    client_ssl.do_handshake()
    print('Server subject is', client_ssl.get_peer_certificate().get_subject())
    client_ssl.close()


if __name__ == '__main__':
    import client
    raise SystemExit(client.main())