Mercurial > templog
comparison py/uploader.py @ 532:9b1d71310c83
better arg parser. seems close to ready
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 11 Apr 2015 23:32:59 +0800 |
parents | 73257bae83ea |
children | 86e638d564b0 |
comparison
equal
deleted
inserted
replaced
531:fd0fd9f947a1 | 532:9b1d71310c83 |
---|---|
1 import json | 1 import json |
2 import hmac | 2 import hmac |
3 import hashlib | |
3 import zlib | 4 import zlib |
4 import binascii | 5 import binascii |
5 import logging | 6 import logging |
6 import asyncio | 7 import asyncio |
7 | 8 |
42 return tosend | 43 return tosend |
43 | 44 |
44 @asyncio.coroutine | 45 @asyncio.coroutine |
45 def send(self, tosend): | 46 def send(self, tosend): |
46 js = json.dumps(tosend) | 47 js = json.dumps(tosend) |
48 if self.server.test_mode(): | |
49 D("Would upload %s to %s" % (js, config.UPDATE_URL)) | |
50 return | |
47 js_enc = binascii.b2a_base64(zlib.compress(js.encode())) | 51 js_enc = binascii.b2a_base64(zlib.compress(js.encode())) |
48 mac = hmac.new(config.HMAC_KEY.encode(), js_enc).hexdigest() | 52 mac = hmac.new(config.HMAC_KEY.encode(), js_enc, hashlib.sha1).hexdigest() |
49 send_data = {'data': js_enc, 'hmac': mac} | 53 send_data = {'data': js_enc, 'hmac': mac} |
50 r = yield from asyncio.wait_for(aiohttp.request('post', config.UPDATE_URL, data=send_data), 60) | 54 r = yield from asyncio.wait_for(aiohttp.request('post', config.UPDATE_URL, data=send_data), 60) |
51 result = yield from asyncio.wait_for(r.text(), 60) | 55 result = yield from asyncio.wait_for(r.text(), 60) |
52 if result != 'OK': | 56 if result != 'OK': |
53 raise Exception("Server returned %s" % result) | 57 raise Exception("Server returned %s" % result) |