class Api: def POST(self, name): r = {'result': None, 'error': None} data = web.data() sender_type = data[0] data = data[1:] if sender_type == '0': #python client q = cPickle.loads(data.decode('zlib')) elif sender_type == '1': #web client q = json.loads(data) try: method = q['method'] if method == 'send_request': request = q['params'][0] user_id = q['params'][1] task_id = q['params'][2] item_id = q['params'][3] params = q['params'][4] r['result'] = web.get_request(web.ctx.env, request, user_id, task_id, item_id, params, sender_type == '1') if task_id == 0: if request == 'exit' and r['result']: sys.exit(0) else: r['result'] = None except Exception, e: print traceback.format_exc() r['error'] = e.message if sender_type == '0': #python client web.header('Content-encoding', 'deflate') return cPickle.dumps(r, 2).encode('zlib') elif sender_type == '1': #web client accepts_gzip = 0 try: if web.ctx.env.get("HTTP_ACCEPT_ENCODING").find("gzip") != -1: accepts_gzip = 1 except: pass buff = json.dumps(r, default=common.json_defaul_handler) web.header('Content-Type', 'application/json') if accepts_gzip: buff = common.compressBuf(buff) web.header('Content-encoding', 'gzip') web.header('Content-Length', str(len(buff))) return buff else: return buff
def create_post_response(self, request, result): response = Response() accepts_gzip = 0 try: if request.environ.get("HTTP_ACCEPT_ENCODING").find("gzip") != -1: accepts_gzip = 1 except: pass buff = json.dumps(result, default=common.json_defaul_handler) response.headers['Content-Type'] = 'application/json' if accepts_gzip: buff = common.compressBuf(buff) response.headers['Content-encoding'] = 'gzip' response.headers['Content-Length'] = str(len(buff)) response.set_data(buff) return response
def prepare_response(self, r): if self.web_request: accepts_gzip = 0 try: if web.ctx.env.get("HTTP_ACCEPT_ENCODING").find("gzip") != -1: accepts_gzip = 1 except: pass buff = json.dumps(r, default=common.json_defaul_handler) web.header('Content-Type', 'application/json') if accepts_gzip: buff = common.compressBuf(buff) web.header('Content-encoding', 'gzip') web.header('Content-Length', str(len(buff))) return buff else: return buff else: web.header('Content-encoding', 'deflate') return cPickle.dumps(r, 2).encode('zlib')