changeset 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 ac77dd191d49
children 0c29d1a16c8c a976793021d2
files py/requirements.txt py/uploader.py
diffstat 2 files changed, 23 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/py/requirements.txt	Thu Jun 26 23:06:10 2014 +0800
+++ b/py/requirements.txt	Fri Jul 25 23:53:38 2014 +0800
@@ -1,7 +1,17 @@
 argparse==1.2.1
-gevent==1.0rc2
-greenlet==0.4.0
+wsgiref==0.1.2
+
+# sha256: v6nYRtuRp9i2o26HNT7tZBx-Pn0L-guZdXltIn8ttOs
+gevent==1.0
+
+# sha256: sWDlVqIuFrrj8_Y__OeJhoLIA82JZFcZL3tU_nT-mR4
+greenlet==0.4.2
+
+# sha256: I9pYnJH1nLfGRNXOXfU51Eg0G9R5kX1t3pc_guJxkUc
 lockfile==0.9.1
+
+# sha256: FmX7Fr_q5y8Wqi3kC8dWYUWL1Ccxp9RjqRGo1er5bAs
 python-daemon==1.6
-smbus==1.1
-wsgiref==0.1.2
+
+# sha256: NkiAJJLpVf_rKPbauGStcUBZ9UOL9nmNgvnUd8ZmrKM
+requests==2.3.0
--- 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))