comparison server/ts.py @ 35:4bfa9b7b2e4b

- fix mac/urllib to work - don't sent 'START' line
author Matt Johnston <matt@ucc.asn.au>
date Sat, 16 Jun 2012 09:03:15 +0800
parents 79124d7d3f79
children 9b5b202129c3
comparison
equal deleted inserted replaced
34:79124d7d3f79 35:4bfa9b7b2e4b
17 import time 17 import time
18 import traceback 18 import traceback
19 import binascii 19 import binascii
20 import hmac 20 import hmac
21 import zlib 21 import zlib
22 import urllib
23 import urllib2
22 24
23 import config 25 import config
24 26
25 from utils import monotonic_time, retry, readline, crc16 27 from utils import monotonic_time, retry, readline, crc16
26 28
55 l = readline(sock) 57 l = readline(sock)
56 if l != 'START\n': 58 if l != 'START\n':
57 print>>sys.stderr, "Bad expected START line '%s'\n" % l.rstrip('\n') 59 print>>sys.stderr, "Bad expected START line '%s'\n" % l.rstrip('\n')
58 return None 60 return None
59 crc = crc16(l, crc) 61 crc = crc16(l, crc)
60 lines.append(l)
61 62
62 while True: 63 while True:
63 l = readline(sock) 64 l = readline(sock)
64 65
65 crc = crc16(l, crc) 66 crc = crc16(l, crc)
120 print>>sys.stderr, "Bad response to clear %s\n" % str(l) 121 print>>sys.stderr, "Bad response to clear %s\n" % str(l)
121 return False 122 return False
122 123
123 def send_results(lines): 124 def send_results(lines):
124 enc_lines = binascii.b2a_base64(zlib.compress('\n'.join(lines))) 125 enc_lines = binascii.b2a_base64(zlib.compress('\n'.join(lines)))
125 hmac.new(config.HMAC_KEY, enc_lines).hexdigest() 126 mac = hmac.new(config.HMAC_KEY, enc_lines).hexdigest()
126 127
127 url_data = urllib.url_encode( ('lines', enc_lines), ('hmac', mac) ) 128 url_data = urllib.urlencode( {'lines': enc_lines, 'hmac': mac} )
128 con = urllib2.urlopen(config.UPDATE_URL, url_data) 129 con = urllib2.urlopen(config.UPDATE_URL, url_data)
129 result = con.read(100) 130 result = con.read(100)
130 if result == 'OK': 131 if result == 'OK':
131 return True 132 return True
132 else: 133 else: