|
49 | 49 | import httplib
|
50 | 50 | import urllib
|
51 | 51 | import logging
|
| 52 | +import random |
| 53 | +import time |
52 | 54 |
|
53 | 55 | # For Python < 2.6, this json module:
|
54 | 56 | # http://pypi.python.org/pypi/python-json
|
@@ -86,6 +88,7 @@ def __init__(self, address, cacheExpiry=5, api=None, logger=None, logLevel=None)
|
86 | 88 | if api is None:
|
87 | 89 | self.api = API()
|
88 | 90 | self.api = getAPI(self.getModel())
|
| 91 | + time.sleep(2) |
89 | 92 | else:
|
90 | 93 | self.api = api
|
91 | 94 |
|
@@ -135,9 +138,15 @@ def _post(self, key, value):
|
135 | 138 | l.debug("Will send params: %s" % params)
|
136 | 139 |
|
137 | 140 | headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
|
138 |
| - conn = self._getConn() |
139 |
| - conn.request("POST", location, params, headers) |
140 |
| - response = conn.getresponse() |
| 141 | + response = None |
| 142 | + count = 0 |
| 143 | + while response is None and count < 5: |
| 144 | + conn = self._getConn() |
| 145 | + conn.request("POST", location, params, headers) |
| 146 | + response = conn.getresponse() |
| 147 | + if response is None: |
| 148 | + time.sleep(count*random.randint(0, 3)) |
| 149 | + count = count + 1 |
141 | 150 | if response.status != 200:
|
142 | 151 | l.error("Error %s while trying to set '%s' with '%s'" % (response.status, location, params))
|
143 | 152 | continue
|
@@ -199,9 +208,15 @@ def _get(self, key, raw=False):
|
199 | 208 | else:
|
200 | 209 | for getter in entry.getters:
|
201 | 210 | # Either data was not cached or cache was expired
|
202 |
| - conn = self._getConn() |
203 |
| - conn.request("GET", getter[0]) |
204 |
| - response = conn.getresponse() |
| 211 | + response = None |
| 212 | + count = 0 |
| 213 | + while response is None and count < 5: |
| 214 | + conn = self._getConn() |
| 215 | + conn.request("GET", getter[0]) |
| 216 | + response = conn.getresponse() |
| 217 | + if response is None: |
| 218 | + time.sleep(count*random.randint(0, 10)) |
| 219 | + count = count + 1 |
205 | 220 | if response.status != 200:
|
206 | 221 | l.warning("Request for '%s' failed (error %s)" % (getter[0], response.status))
|
207 | 222 | response = None
|
@@ -357,7 +372,7 @@ def setCloudMode(self, value):
|
357 | 372 | def main():
|
358 | 373 | import sys
|
359 | 374 | addr = sys.argv[1]
|
360 |
| - t = TStat(addr) |
| 375 | + t = TStat(addr, api=API_CT50v109()) |
361 | 376 | for cmd in sys.argv[2:]:
|
362 | 377 | result = eval("t.%s(raw=True)" % cmd)
|
363 | 378 | #print "%s: %s" % (cmd, result)
|
|
0 commit comments