1 #!/usr/bin/env python2.5
3 # Copyright (c) 2011 Neal H. Walfield <neal@walfield.org>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 class ProgressSocket(object):
24 Monitor what is being sent and received.
26 def __init__(self, socket, connection):
28 self.connection = connection
30 def __getattribute__(self, attr):
31 # print "%s.__getattribute__(%s)" % (self.__class__.__name__, attr)
37 while sent < len (data):
38 remaining = len (data) - sent
44 self.socket.sendall(data[sent:sent+amount])
46 self.connection.stats['sent'] += amount
47 self.connection.opener.stats['sent'] += amount
49 if self.connection.callback is not None:
50 self.connection.callback ()
52 def read(*args, **kwargs):
53 data = self.socket.read (*args, **kwargs)
54 # print "GOT: %s" % (data[0:240],)
55 self.connection.stats['received'] += len (data)
56 self.connection.opener.stats['received'] += len (data)
57 if self.connection.callback is not None:
58 self.connection.callback ()
61 if attr == 'send' or attr == 'sendall':
67 return super (ProgressSocket, self).__getattribute__(attr)
68 except AttributeError:
69 socket = super (ProgressSocket, self).__getattribute__('socket')
70 return socket.__getattribute__(attr)
72 def makefile(self, mode, bufsize):
73 return ProgressSocket (socket=self.socket.makefile(mode, bufsize),
74 connection=self.connection)
77 return self.socket.close ()
79 def HTTPProgressConnectionBuilder(callback, opener):
80 class HTTPProgressConnection(httplib.HTTPConnection):
81 def __init__(self, *args, **kwargs):
84 return httplib.HTTPConnection.__init__ (self, *args, **kwargs)
86 def putrequest(self, method, url, *args, **kwargs):
89 return httplib.HTTPConnection.putrequest (
90 self, method, url, *args, **kwargs)
93 httplib.HTTPConnection.connect(self)
95 self.sock = ProgressSocket(socket=self.sock,
98 HTTPProgressConnection.callback = callback
99 HTTPProgressConnection.opener = opener
100 HTTPProgressConnection.stats \
101 = {'sent': 0, 'received': 0, 'started':time.time()}
102 return HTTPProgressConnection
104 class HTTPProgressHandler(urllib2.HTTPHandler):
105 def __init__(self, callback):
106 self.callback = callback
107 self.stats = {'sent': 0, 'received': 0, 'started':time.time()}
108 return urllib2.HTTPHandler.__init__(self)
110 def http_open(self, request):
112 HTTPProgressConnectionBuilder(self.callback, self),
115 if __name__ == '__main__':
116 def callback(connection):
118 if connection.method:
119 req += connection.method + " "
120 req += connection.host + ':' + str (connection.port)
122 req += connection.url
124 cstats = connection.stats
125 ostats = connection.opener.stats
127 print (("%s: connection: %d sent, %d received: %d kb/s; "
128 + "opener: %d sent, %d received, %d kb/s")
130 cstats['sent'], cstats['received'],
131 ((cstats['sent'] + cstats['received'])
132 / (time.time() - cstats['started']) / 1024),
133 ostats['sent'], ostats['received'],
134 ((ostats['sent'] + ostats['received'])
135 / (time.time() - ostats['started']) / 1024)))
137 opener = urllib2.build_opener(HTTPProgressHandler(callback))
139 data = opener.open ('http://google.com')
142 downloaded += len (d)
143 print "Document is %d bytes in size" % (downloaded,)