def render(self, request): request.setHeader("Content-type", 'application/json; charset=utf-8') try: # handle incoming data if settings.DEBUG: print '\n', request.path content = request.content.read() if content: request.parsed_content = json.decode(content) if settings.DEBUG: print content if settings.DEBUG: print db.session # handle outgoing data body = SiteResource.render(self, request) except Http404, h: if settings.DEBUG: traceback.print_exc() request.setResponseCode(http.NOT_FOUND) body = {'error': str(h)}
def urlopen(path, data=None, code=http.OK): url = server_url + path json_data = data and json.encode(data) or None req = urllib.Request(url, json_data, headers) response = None while True: # try connecting until server is up try: response = urllib.urlopen(req) break except urllib.HTTPError, he: # anything but 200 OK raises HTTPError # so look for desired response code here if he.code == code: return json.decode(he.read()) raise except urllib.URLError, ue: if ue.reason.args[0] in (10061, 111): # connection refused # wait for server to be up time.sleep(0.5) continue raise
def encode_decode_datetime(self, d): s = json.encode(d) self.assertEquals(s, u'"%s"' % (str(d)[:-7])) d2 = json.decode(s) self.assertEquals(d2, unicode(d)[:-7])
def encode_decode_decimal(self, d): s = json.encode(d) self.assertEquals(s, u'"%s"' % d) d2 = json.decode(s) self.assertEquals(d2, unicode(d))
except urllib.HTTPError, he: # anything but 200 OK raises HTTPError # so look for desired response code here if he.code == code: return json.decode(he.read()) raise except urllib.URLError, ue: if ue.reason.args[0] in (10061, 111): # connection refused # wait for server to be up time.sleep(0.5) continue raise if response: response = response.read() if response: json_response = json.decode(response) return json_response class ClientTest(unittest.TestCase): acct_decimal_fields = ('balance', 'upper_limit', 'lower_limit') def setUp(self): db.reset() self.server = make_server() def tearDown(self): if self.server: try: # windows PROCESS_TERMINATE = 1 handle = ctypes.windll.kernel32.OpenProcess( PROCESS_TERMINATE, False, self.server.pid)