comparison py/uploader.py @ 513:56cdea43b366

use requests rather than urllib2, better ipv6->ipv4 fallback
author Matt Johnston <matt@ucc.asn.au>
date Fri, 25 Jul 2014 23:53:38 +0800
parents 1865674794c3
children 16a83e2c97a0
comparison
equal deleted inserted replaced
512:ac77dd191d49 513:56cdea43b366
1 import json 1 import json
2 import hmac 2 import hmac
3 import zlib 3 import zlib
4 import binascii 4 import binascii
5 import urllib 5 import logging
6 import urllib2
7 6
8 import gevent 7 import gevent
8 import requests
9 9
10 import config 10 import config
11 from utils import L,D,EX,W,E 11 from utils import L,D,EX,W,E
12 import utils 12 import utils
13 13
14 class Uploader(gevent.Greenlet): 14 class Uploader(gevent.Greenlet):
15 def __init__(self, server): 15 def __init__(self, server):
16 gevent.Greenlet.__init__(self) 16 gevent.Greenlet.__init__(self)
17 self.server = server 17 self.server = server
18
19 requests_log = logging.getLogger("requests")
20 requests_log.setLevel(logging.WARNING)
18 21
19 def _run(self): 22 def _run(self):
20 gevent.sleep(5) 23 gevent.sleep(5)
21 while True: 24 while True:
22 self.do() 25 self.do()
42 45
43 def send(self, tosend): 46 def send(self, tosend):
44 js = json.dumps(tosend) 47 js = json.dumps(tosend)
45 js_enc = binascii.b2a_base64(zlib.compress(js)) 48 js_enc = binascii.b2a_base64(zlib.compress(js))
46 mac = hmac.new(config.HMAC_KEY, js_enc).hexdigest() 49 mac = hmac.new(config.HMAC_KEY, js_enc).hexdigest()
47 url_data = urllib.urlencode( {'data': js_enc, 'hmac': mac} ) 50 send_data = {'data': js_enc, 'hmac': mac}
48 con = urllib2.urlopen(config.UPDATE_URL, url_data) 51 r = requests.post(config.UPDATE_URL, data=send_data)
49 result = con.read(100) 52 result = r.text
50 if result != 'OK': 53 if result != 'OK':
51 raise Exception("Server returned %s" % result) 54 raise Exception("Server returned %s" % result)
52 55
53 def do(self): 56 def do(self):
54 readings = self.server.take_readings() 57 readings = self.server.take_readings()
56 tosend = self.get_tosend(readings) 59 tosend = self.get_tosend(readings)
57 nreadings = len(readings) 60 nreadings = len(readings)
58 self.send(tosend) 61 self.send(tosend)
59 readings = None 62 readings = None
60 D("Sent updated %d readings" % nreadings) 63 D("Sent updated %d readings" % nreadings)
61 except urllib2.HTTPError, e: 64 except requests.exceptions.RequestException, e:
62 E("Error in uploader: %s" % str(e)) 65 E("Error in uploader: %s" % str(e))
63 except Exception, e: 66 except Exception, e:
64 EX("Error in uploader: %s" % str(e)) 67 EX("Error in uploader: %s" % str(e))
65 finally: 68 finally:
66 if readings is not None: 69 if readings is not None: