import warnings
import urllib
import urllib2
-import md5
+import hashlib
+import time
_use_simplejson = False
try:
def _sign(self, params):
"Sign the parameters with MD5 hash"
pairs = ''.join(['%s%s' % (k, v) for (k, v) in sortedItems(params)])
- return md5.md5(self._secret+pairs).hexdigest()
+ return hashlib.md5(self._secret+pairs).hexdigest()
@staticmethod
def open_url(url, queryArgs=None):
params['api_sig'] = self._sign(params)
connection = self.open_url(SERVICE_URL, params)
- json = connection.read()
+
+ # It appears that urllib uses the non-blocking variant of file objects
+ # which means reads might not always be complete, so grabbing as much
+ # of the data as possible with a sleep in between to give it more time
+ # to grab data.
+ chunks = []
+ chunk = connection.read()
+ while chunk:
+ chunks.append(chunk)
+ time.sleep(1)
+ chunk = connection.read()
+ json = "".join(chunks)
+ if "Content-Length" in connection.info():
+ assert len(json) == int(connection.info()["Content-Length"]), "The packet header promised %s of data but only was able to read %s of data" % (
+ len(json),
+ connection.info()["Content-Length"],
+ )
+
data = DottedDict('ROOT', parse_json(json))
rsp = data.rsp