~ubuntu-branches/ubuntu/maverick/m2crypto/maverick

« back to all changes in this revision

Viewing changes to demo/ssl/c.py

  • Committer: Bazaar Package Importer
  • Author(s): Dima Barsky
  • Date: 2004-03-30 21:54:28 UTC
  • Revision ID: james.westby@ubuntu.com-20040330215428-7zulfxz4xt31w2xr
Tags: upstream-0.13
ImportĀ upstreamĀ versionĀ 0.13

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
"""C programming in Python. Have SWIG sweat the pointers. ;-)
 
4
 
 
5
Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved."""
 
6
 
 
7
RCS_id='$Id: c.py,v 1.4 2003/06/22 17:14:46 ngps Exp $'
 
8
 
 
9
from socket import *
 
10
import sys
 
11
 
 
12
from M2Crypto import SSL, m2
 
13
 
 
14
HOST = '127.0.0.1'
 
15
PORT = 9443
 
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'
 
18
 
 
19
 
 
20
def c_10():
 
21
    c_style(HOST, PORT, req_10) 
 
22
 
 
23
 
 
24
def c_11():
 
25
    c_style(HOST, PORT, req_11) 
 
26
 
 
27
 
 
28
def c_style(HOST, PORT, req):
 
29
 
 
30
    # Set up SSL context.
 
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')
 
34
 
 
35
    # Make the socket connection.
 
36
    s = socket(AF_INET, SOCK_STREAM)
 
37
    s.connect((HOST, PORT))
 
38
 
 
39
    # Set up the SSL connection.
 
40
    sbio = m2.bio_new_socket(s.fileno(), 0)
 
41
    ssl = m2.ssl_new(ctx)
 
42
    m2.ssl_set_bio(ssl, sbio, sbio)
 
43
    m2.ssl_connect(ssl)
 
44
    sslbio = m2.bio_new(m2.bio_f_ssl())
 
45
    m2.bio_set_ssl(sslbio, ssl, 0)
 
46
 
 
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)
 
50
 
 
51
    # Send the request.
 
52
    m2.bio_write(sslbio, req)
 
53
 
 
54
    # Receive the response.
 
55
    while 1:
 
56
        data = m2.bio_gets(topbio, 4096)
 
57
        if not data: break
 
58
        sys.stdout.write(data)
 
59
 
 
60
    # Cleanup. May be missing some necessary steps. ;-|
 
61
    m2.bio_pop(topbio)
 
62
    m2.bio_free(iobuf)
 
63
    m2.ssl_shutdown(ssl)
 
64
    m2.ssl_free(ssl)
 
65
    m2.ssl_ctx_free(ctx)
 
66
    s.close()
 
67
 
 
68
 
 
69
if __name__ == '__main__':
 
70
    c_10()
 
71
    c_11()
 
72