def _send_rpc_requests(self, batch): rpcs = [] id_to_key_map = {} query_params = {} """Build up a list of RPC calls. Also, create a mapping of RPC request id's to batch keys in order to populate the batch object with the responses. """ for key, request in batch.requests.iteritems(): query_params.update(request.get_query_params()) rpc_body = request.get_rpc_body() rpc_id = rpc_body.get('id') id_to_key_map[rpc_id] = key rpcs.append(rpc_body) http_request = http.Request(self.config.server_rpc_base, method='POST', signed_params=query_params, post_body=rpcs) http_response = self._send_http_request(http_request) json = self._handle_response(http_response) """Pull out all of the results and insert them into the batch object.""" for response in json: id = response.get('id') key = id_to_key_map[id] if 'error' in response: code = response.get('error').get('code') message = response.get('error').get('message') error = BadResponseError(code, message) batch._set_data(key, error) else: json = response.get('data') request = batch.requests[key] batch._set_data(key, request.process_json(json))
def delete_bulk(self, query): """ Bulk delete. Returns a Response dict-like object in the form of { 'errors': [<items>], 'success': [<items>] } """ if not isinstance(query, dict) \ or not isinstance(query.get('q', None), dict): err_msg = 'query must be a dictionary and contain a q dict' raise Error(http_status_code=400, error=err_msg, code=0, response_dict={}) q = query.get('q') if not len(q.keys()): err_msg = 'query dictionary must be a non empty' raise Error(http_status_code=400, error=err_msg, code=0, response_dict={}) headers = self._get_headers(add_content=True) request = http.Request(uri=self._get_uri(query=query), method='DELETE', headers=headers) return self.client.request(request)
def authenticate(self, username, password): uri = '%s/users/auth_token' % self.base_uri enc_creds = base64.b64encode('%s:%s' % (username, password)) headers = {'Authorization': 'Basic %s' % enc_creds} request = http.Request(uri=uri, method='POST', headers=headers) self.auth_token = self.request(request)['name'] return True
def update(self, id, content, query=None): """ API PUT """ headers = self._get_headers(add_content=True) request = http.Request(uri=self._get_uri(id, query), method='PUT', body=json.dumps(content), headers=headers) return self.client.request(request)
def delete(self, id): """ API DELETE """ headers = self._get_headers(add_content=True) request = http.Request(uri=self._get_uri(id), method='DELETE', headers=headers) return self.client.request(request)
def create(self, content): """ API POST """ headers = self._get_headers(add_content=True) request = http.Request(uri=self._get_uri(), method='POST', body=json.dumps(content), headers=headers) return self.client.request(request)
def make_http_request(self, url_base, query_params=None): """Generates a http.Request object for the UrlFetch interface. Args: url_base: str The base REST URL. Returns: The http.Request object. """ # Ensure that there is a path separator. if url_base[-1] is not '/' and self.path[0] is not '/': url_base = url_base + '/' url = url_base + self.path self.params.update(query_params) return http.Request(url, method=self.method, signed_params=self.params)
def get_request(dir, pcap_id, label, req_idx, req_len): client, server = tuple(label.split('2')) # 打开客户端发送的数据,找到请求,处理完后关闭 req_file = open( os.path.join(dir, ('%s_%s2%s_contents.dat' % (pcap_id, client, server)))) req_file.seek(req_idx) req_str = req_file.read(req_len) req_file.close() # 解析请求,忽略请求体,只解析请求头部 req = http.Request() req.unpack(req_str, False) return req
def decode(self, state, statemgr): """ Takes an instance of flow.flow_state, and an instance of flow.flow_state_manager """ self.statemgr = statemgr state.open(flags="rb", statemgr=self.statemgr) d = state.fp.readlines() state.close() #print "decode:state ", state.fname if len(d) == 0: return d = self.check_data(d) t, req = self.determineType(d) if (t, req) == (None, None): # binary data return d = "".join(d) r = None f = state.flow if t == 'response': try: r = http.Response(d) r.request = req if not hasattr(r, "data"): setattr(r, "data", None) state.decoded = r except (dpkt.Error, ValueError): try: # bad data, try lax parsing state.open(flags="rb", statemgr=self.statemgr) l = state.fp.readline() headers = http.parse_headers(state.fp) r = http.Message() r.headers = headers r.body = state.fp.readlines() r.data = None r.status = "-" r.request = req state.decoded = r state.close() except dpkt.Error: print "response failed to decode: %s " % state.fname pass if t == 'request': try: r = http.Request(d) state.decoded = r r.request = req if not getattr(r, "data"): r.data = None except dpkt.Error: try: # bad data, so let's try some laxer parsing state.open(flags="rb", statemgr=self.statemgr) l = state.fp.readline() headers = http.parse_headers(state.fp) r = http.Message() r.headers = headers r.body = state.fp.readlines() r.request = req r.data = None state.decoded = r state.close() # frig up some stuff for the logging h = req.split() r.method = h[0].strip() r.uri = h[1].strip() except dpkt.Error: print "request failed to decode: %s " % state.fname pass if r: state.decoded = r else: return if t is not None: self.extractHeaders(state, d) rs = self.statemgr.find_flow_state(freverse(state.flow)) if not rs: # haven't seen other half - just fake something so that at least the request gets logged. if t == 'request': dummy_response = http.Response() dummy_response.__dict__['status'] = '-' self._add_log_entry(r, dummy_response, f.src, f.dst, state.ts) return if rs.decoded: self._renameFlow(state, t) else: self.decode(rs, self.statemgr) if rs.decoded: if t == 'request': self._add_log_entry(r, rs.decoded, f.src, f.dst, state.ts) elif t == 'response': self._add_log_entry(rs.decoded, r, f.dst, f.src, rs.ts)
def replay(self): req = http.Request(self.header) req.timeout = self.timeout method = getattr(req, self.method.lower()) return method(self.url, self.params)
def _get(self, obj=None, query=None): headers = self._get_headers(add_content=True) request = http.Request(uri=self._get_uri(obj, query), headers=headers) return self.client.request(request)