~stepankk/pyopenssl/bug-845445

« back to all changes in this revision

Viewing changes to examples/sni/client.py

  • Committer: Jean-Paul Calderone
  • Date: 2011-06-06 12:33:31 UTC
  • mfrom: (153.1.4 sni)
  • Revision ID: exarkun@divmod.com-20110606123331-vm00hkfitja61m7c
Add client and server support for SNI.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) Jean-Paul Calderone
 
2
# See LICENSE for details.
 
3
 
 
4
if __name__ == '__main__':
 
5
    import client
 
6
    raise SystemExit(client.main())
 
7
 
 
8
from sys import argv, stdout
 
9
from socket import socket
 
10
 
 
11
from OpenSSL.SSL import TLSv1_METHOD, Context, Connection
 
12
 
 
13
def main():
 
14
    """
 
15
    Connect to an SNI-enabled server and request a specific hostname, specified
 
16
    by argv[1], of it.
 
17
    """
 
18
    if len(argv) < 2:
 
19
        print 'Usage: %s <hostname>' % (argv[0],)
 
20
        return 1
 
21
 
 
22
    client = socket()
 
23
 
 
24
    print 'Connecting...',
 
25
    stdout.flush()
 
26
    client.connect(('127.0.0.1', 8443))
 
27
    print 'connected', client.getpeername()
 
28
 
 
29
    client_ssl = Connection(Context(TLSv1_METHOD), client)
 
30
    client_ssl.set_connect_state()
 
31
    client_ssl.set_tlsext_host_name(argv[1])
 
32
    client_ssl.do_handshake()
 
33
    print 'Server subject is', client_ssl.get_peer_certificate().get_subject()
 
34
    client_ssl.close()
 
35