3
"""Demo for client-side ssl_dispatcher usage. Note that connect()
4
is blocking. (Need fix?)
6
This isn't really a HTTPS client; it's just a toy.
8
Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved."""
10
RCS_id='$Id: https_cli_async.py,v 1.3 2003/06/22 17:19:30 ngps Exp $'
12
import asyncore, sys, time
13
from M2Crypto import Rand, SSL
15
class https_client(SSL.ssl_dispatcher):
17
def __init__(self, host, path, ssl_ctx):
18
SSL.ssl_dispatcher.__init__(self)
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))
26
def handle_connect(self):
32
def handle_read(self):
39
sys.stdout.write('%s: total: %5d\n' % (self.path, self._count,))
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)
49
except SSL.SSLError, why:
50
print 'handle_read:', why
55
return (len(self.buffer) > 0)
57
def handle_write(self):
59
sent = self.send(self.buffer)
60
self.buffer = self.buffer[sent:]
61
except SSL.SSLError, why:
62
print 'handle_write:', why
66
if __name__ == '__main__':
67
Rand.load_file('../randpool.dat', -1)
69
url = ('/jdk118/api/u-names.html',
70
'/postgresql/xfunc-c.html',
71
'/python2.1/modindex.html')
73
https_client('', u, ctx)
75
Rand.save_file('../randpool.dat')
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