blob: 6bae488806f8f98e25ff8b77271c3e9ca82e3a03 [file] [log] [blame]
Jean-Paul Calderone3de9f622008-03-12 14:12:19 -04001# -*- coding: latin-1 -*-
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05002# client.py
3#
4# Copyright (C) 2001 Martin Sjögren and AB Strakt, All rights reserved
5#
6# $Id: client.py,v 1.7 2002/08/15 12:20:46 martin Exp $
7#
8"""
9Simple SSL client, using blocking I/O
10"""
11
12from OpenSSL import SSL
13import sys, os, select, socket
14
15def verify_cb(conn, cert, errnum, depth, ok):
16 # This obviously has to be updated
17 print 'Got certificate: %s' % cert.get_subject()
18 return ok
19
20if len(sys.argv) < 3:
21 print 'Usage: python[2] client.py HOST PORT'
22 sys.exit(1)
23
24dir = os.path.dirname(sys.argv[0])
25if dir == '':
26 dir = os.curdir
27
28# Initialize context
29ctx = SSL.Context(SSL.SSLv23_METHOD)
30ctx.set_verify(SSL.VERIFY_PEER, verify_cb) # Demand a certificate
31ctx.use_privatekey_file (os.path.join(dir, 'client.pkey'))
32ctx.use_certificate_file(os.path.join(dir, 'client.cert'))
33ctx.load_verify_locations(os.path.join(dir, 'CA.cert'))
34
35# Set up client
36sock = SSL.Connection(ctx, socket.socket(socket.AF_INET, socket.SOCK_STREAM))
37sock.connect((sys.argv[1], int(sys.argv[2])))
38
39while 1:
40 line = sys.stdin.readline()
41 if line == '':
42 break
43 try:
44 sock.send(line)
45 sys.stdout.write(sock.recv(1024))
46 sys.stdout.flush()
47 except SSL.Error:
48 print 'Connection died unexpectedly'
49 break
50
51
52sock.shutdown()
53sock.close()