Jean-Paul Calderone | 7fb6b21 | 2011-06-06 08:31:28 -0400 | [diff] [blame] | 1 | # Copyright (C) Jean-Paul Calderone |
| 2 | # See LICENSE for details. |
| 3 | |
Alex Gaynor | ca749b5 | 2019-02-10 17:23:19 -0500 | [diff] [blame] | 4 | from __future__ import print_function |
| 5 | |
Jean-Paul Calderone | 7fb6b21 | 2011-06-06 08:31:28 -0400 | [diff] [blame] | 6 | from sys import argv, stdout |
| 7 | from socket import socket |
| 8 | |
| 9 | from OpenSSL.SSL import TLSv1_METHOD, Context, Connection |
| 10 | |
Hynek Schlawack | 8b7e455 | 2016-03-13 07:51:09 +0100 | [diff] [blame] | 11 | |
Jean-Paul Calderone | 7fb6b21 | 2011-06-06 08:31:28 -0400 | [diff] [blame] | 12 | def main(): |
| 13 | """ |
| 14 | Connect to an SNI-enabled server and request a specific hostname, specified |
| 15 | by argv[1], of it. |
| 16 | """ |
| 17 | if len(argv) < 2: |
Alex Gaynor | ca749b5 | 2019-02-10 17:23:19 -0500 | [diff] [blame] | 18 | print('Usage: %s <hostname>' % (argv[0],)) |
Jean-Paul Calderone | 7fb6b21 | 2011-06-06 08:31:28 -0400 | [diff] [blame] | 19 | return 1 |
| 20 | |
| 21 | client = socket() |
| 22 | |
Alex Gaynor | ca749b5 | 2019-02-10 17:23:19 -0500 | [diff] [blame] | 23 | print('Connecting...', end="") |
Jean-Paul Calderone | 7fb6b21 | 2011-06-06 08:31:28 -0400 | [diff] [blame] | 24 | stdout.flush() |
| 25 | client.connect(('127.0.0.1', 8443)) |
Alex Gaynor | ca749b5 | 2019-02-10 17:23:19 -0500 | [diff] [blame] | 26 | print('connected', client.getpeername()) |
Jean-Paul Calderone | 7fb6b21 | 2011-06-06 08:31:28 -0400 | [diff] [blame] | 27 | |
| 28 | client_ssl = Connection(Context(TLSv1_METHOD), client) |
| 29 | client_ssl.set_connect_state() |
| 30 | client_ssl.set_tlsext_host_name(argv[1]) |
| 31 | client_ssl.do_handshake() |
Alex Gaynor | ca749b5 | 2019-02-10 17:23:19 -0500 | [diff] [blame] | 32 | print('Server subject is', client_ssl.get_peer_certificate().get_subject()) |
Jean-Paul Calderone | 7fb6b21 | 2011-06-06 08:31:28 -0400 | [diff] [blame] | 33 | client_ssl.close() |
| 34 | |
Hynek Schlawack | 8b7e455 | 2016-03-13 07:51:09 +0100 | [diff] [blame] | 35 | |
| 36 | if __name__ == '__main__': |
| 37 | import client |
| 38 | raise SystemExit(client.main()) |