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

« back to all changes in this revision

Viewing changes to demo/ssl/https_cli_async.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
"""Demo for client-side ssl_dispatcher usage. Note that connect() 
 
4
is blocking. (Need fix?) 
 
5
 
 
6
This isn't really a HTTPS client; it's just a toy.
 
7
 
 
8
Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved."""
 
9
 
 
10
RCS_id='$Id: https_cli_async.py,v 1.3 2003/06/22 17:19:30 ngps Exp $'
 
11
 
 
12
import asyncore, sys, time
 
13
from M2Crypto import Rand, SSL
 
14
 
 
15
class https_client(SSL.ssl_dispatcher):
 
16
 
 
17
    def __init__(self, host, path, ssl_ctx):
 
18
        SSL.ssl_dispatcher.__init__(self)
 
19
        self.path = path
 
20
        self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % self.path
 
21
        self.create_socket(ssl_ctx)
 
22
        self.socket.connect((host, 9443))
 
23
        self._can_read = 1
 
24
        self._count = 0
 
25
 
 
26
    def handle_connect(self):
 
27
        pass
 
28
 
 
29
    def readable(self):
 
30
        return self._can_read
 
31
 
 
32
    def handle_read(self):
 
33
        try:
 
34
            result = self.recv()
 
35
            if result is None:
 
36
                return
 
37
            elif result == '':
 
38
                self._can_read = 0
 
39
                sys.stdout.write('%s: total: %5d\n' % (self.path, self._count,))
 
40
                sys.stdout.flush()
 
41
                self.close()
 
42
            else:
 
43
                #print result
 
44
                l = len(result)
 
45
                self._count = self._count + l
 
46
                display = (time.time(), l, self.path)
 
47
                sys.stdout.write('%14.3f: read %5d from %s\n' % display)
 
48
                sys.stdout.flush()
 
49
        except SSL.SSLError, why:
 
50
            print 'handle_read:', why
 
51
            self.close()
 
52
            raise
 
53
 
 
54
    def writable(self):
 
55
        return (len(self.buffer) > 0)
 
56
 
 
57
    def handle_write(self):
 
58
        try:
 
59
            sent = self.send(self.buffer)
 
60
            self.buffer = self.buffer[sent:]
 
61
        except SSL.SSLError, why:
 
62
            print 'handle_write:', why
 
63
            self.close()
 
64
 
 
65
 
 
66
if __name__ == '__main__':
 
67
    Rand.load_file('../randpool.dat', -1) 
 
68
    ctx = SSL.Context()
 
69
    url = ('/jdk118/api/u-names.html', 
 
70
        '/postgresql/xfunc-c.html', 
 
71
        '/python2.1/modindex.html')
 
72
    for u in url:
 
73
        https_client('', u, ctx)
 
74
    asyncore.loop()
 
75
    Rand.save_file('../randpool.dat')
 
76
 
 
77
 
 
78
# Here's a sample output. Server is Apache+mod_ssl on localhost.
 
79
# $ python https_cli_async.py 
 
80
# 991501090.682: read   278 from /python2.1/modindex.html
 
81
# 991501090.684: read   278 from /postgresql/xfunc-c.html
 
82
# 991501090.742: read  4096 from /postgresql/xfunc-c.html
 
83
# 991501090.743: read  4096 from /postgresql/xfunc-c.html
 
84
# 991501090.744: read  4096 from /postgresql/xfunc-c.html
 
85
# 991501090.744: read  4096 from /postgresql/xfunc-c.html
 
86
# 991501090.755: read  4096 from /postgresql/xfunc-c.html
 
87
# 991501090.756: read   278 from /jdk118/api/u-names.html
 
88
# 991501090.777: read  4096 from /postgresql/xfunc-c.html
 
89
# 991501090.778: read  4096 from /postgresql/xfunc-c.html
 
90
# 991501090.778: read  4096 from /postgresql/xfunc-c.html
 
91
# 991501090.782: read  4096 from /postgresql/xfunc-c.html
 
92
# 991501090.813: read  4096 from /python2.1/modindex.html
 
93
# 991501090.839: read  4096 from /jdk118/api/u-names.html
 
94
# 991501090.849: read  4096 from /python2.1/modindex.html
 
95
# 991501090.873: read  3484 from /postgresql/xfunc-c.html
 
96
# 991501090.874: read  4096 from /jdk118/api/u-names.html
 
97
# 991501090.874: read  4096 from /python2.1/modindex.html
 
98
#/postgresql/xfunc-c.html: total: 40626
 
99
# 991501090.886: read  4096 from /jdk118/api/u-names.html
 
100
# 991501090.886: read  2958 from /python2.1/modindex.html
 
101
# 991501090.887: read  4096 from /jdk118/api/u-names.html
 
102
#/python2.1/modindex.html: total: 15524
 
103
# 991501090.893: read  4096 from /jdk118/api/u-names.html
 
104
# 991501090.894: read  2484 from /jdk118/api/u-names.html
 
105
#/jdk118/api/u-names.html: total: 23242
 
106
# $
 
107
 
 
108