3
"""C programming in Python. Have SWIG sweat the pointers. ;-)
5
Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved."""
7
RCS_id='$Id: c.py,v 1.4 2003/06/22 17:14:46 ngps Exp $'
12
from M2Crypto import SSL, m2
16
req_10 = 'GET / HTTP/1.0\r\n\r\n'
17
req_11 = 'GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n'
21
c_style(HOST, PORT, req_10)
25
c_style(HOST, PORT, req_11)
28
def c_style(HOST, PORT, req):
31
ctx = m2.ssl_ctx_new(m2.sslv3_method())
32
m2.ssl_ctx_use_cert(ctx, 'client.pem')
33
m2.ssl_ctx_use_privkey(ctx, 'client.pem')
35
# Make the socket connection.
36
s = socket(AF_INET, SOCK_STREAM)
37
s.connect((HOST, PORT))
39
# Set up the SSL connection.
40
sbio = m2.bio_new_socket(s.fileno(), 0)
42
m2.ssl_set_bio(ssl, sbio, sbio)
44
sslbio = m2.bio_new(m2.bio_f_ssl())
45
m2.bio_set_ssl(sslbio, ssl, 0)
47
# Push a buffering BIO over the SSL BIO.
48
iobuf = m2.bio_new(m2.bio_f_buffer())
49
topbio = m2.bio_push(iobuf, sslbio)
52
m2.bio_write(sslbio, req)
54
# Receive the response.
56
data = m2.bio_gets(topbio, 4096)
58
sys.stdout.write(data)
60
# Cleanup. May be missing some necessary steps. ;-|
69
if __name__ == '__main__':