diff py/uploader.py @ 215:4d82099d1fe0

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 f4aabbf0da67
children 16a83e2c97a0
line wrap: on
line diff
--- a/py/uploader.py	Thu Jun 26 23:06:10 2014 +0800
+++ b/py/uploader.py	Fri Jul 25 23:53:38 2014 +0800
@@ -2,10 +2,10 @@
 import hmac
 import zlib
 import binascii
-import urllib
-import urllib2
+import logging
 
 import gevent
+import requests
 
 import config
 from utils import L,D,EX,W,E
@@ -16,6 +16,9 @@
         gevent.Greenlet.__init__(self)
         self.server = server
 
+        requests_log = logging.getLogger("requests")
+        requests_log.setLevel(logging.WARNING)
+
     def _run(self):
         gevent.sleep(5)
         while True:
@@ -44,9 +47,9 @@
         js = json.dumps(tosend)
         js_enc = binascii.b2a_base64(zlib.compress(js))
         mac = hmac.new(config.HMAC_KEY, js_enc).hexdigest()
-        url_data = urllib.urlencode( {'data': js_enc, 'hmac': mac} )
-        con = urllib2.urlopen(config.UPDATE_URL, url_data)
-        result = con.read(100)
+        send_data = {'data': js_enc, 'hmac': mac}
+        r = requests.post(config.UPDATE_URL, data=send_data)
+        result = r.text
         if result != 'OK':
             raise Exception("Server returned %s" % result)
 
@@ -58,7 +61,7 @@
             self.send(tosend)
             readings = None
             D("Sent updated %d readings" % nreadings)
-        except urllib2.HTTPError, e:
+        except requests.exceptions.RequestException, e:
             E("Error in uploader: %s" % str(e))
         except Exception, e:
             EX("Error in uploader: %s" % str(e))