def get_test_request(): req = HttpRequest() req.my_db = get_test_db() req.my_img_db = get_test_img_db() paras = req.parameters _user = get_test_user_dto() # 前端或者前序流程中(比如测试案例)没有上送跟踪号,就生成跟踪号 paras['trace_id'] = get_trace_id() paras['reason'] = 'overtime' paras['userInfo'] = dict() user_info = paras['userInfo'] user_info['login_name'] = _user.login_name user_info['name'] = _user.name user_info['dept_name'] = _user.depart user_info['mobile'] = _user.mobile user_info['memo'] = _user.memo user_info['org'] = _user.org user_info['id'] = _user.id _assets = get_test_book_assets_dto() paras["code"] = _assets.code paras["name"] = _assets.name paras["memo"] = _assets.memo paras["image"] = _assets.image paras['category'] = _assets.category paras['limit_time'] = _assets.limit_time return req
def get_image(req: HttpRequest, code): b_ret = False op_type = Const.OpType.查询 if code: assets_impl = AssetsImpl(_db=req.my_db, _img_db=req.my_img_db) tmp_code, header, image = assets_impl.get_image(code=code) if image: content_type = "application/octet-stream" if header: if type(header) == bytes or type(header) == bytearray: header = header.decode("utf-8") tmp = re.findall('Content-Type:.*', header, re.I) if tmp: content_type = re.sub('Content-Type:', '', tmp[0], re.I) req.res_head['Content-Type'] = content_type req.res_body = image b_ret = True else: req.res_command = ResponseCode.NOT_FOUND package_body(req=req, status=Const.OpStatus.成功, op_type=op_type, message='资产代码:{0}, 没有图像记录或者图像记录格式不对'.format(code, )) else: req.res_command = ResponseCode.BAD_REQUEST package_body(req=req, status=Const.OpStatus.失败, op_type=op_type, message='没有上送资产编码') return b_ret
def main1(): host = "httpbin.org" sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((host, 80)) # https://tools.ietf.org/html/rfc7230#section-6.1 req = HttpRequest("GET", "/", headers={ "Host": host, "Connection": "close" }) f = sock.makefile() req.write_to(f) ''' response = HttpResponse.read_from(f) print response.status_code print "tästä alkaa responsen body" print response.body ''' response = HttpResponse(200, "OK", "Olen torso\n", headers={"Host": "localhost"}) response.write_to(sys.stdout) f.close() sock.close()
def main(): # määritellään socketin sekä headerin host host = "httpbin.org" # luodaan uusi socket-objecti (ipv4/tcp) sukka = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sukka.connect((host, 80)) req = HttpRequest("GET", "/", headers={"Host": host, "Connection": "close"}) # Luodaan aikaisemmin luodun socket-objectin avulla uusi f-niminen fileobjecti makefile() metodia käyttäen # makefile() metodi palauttaa siis uuden fileobjectin joka on sidottu socket-objectiin f = sukka.makefile() req.write_to(f) # Kutsutaan HttpRequest-luokassa määriteltyä metodia write_to() (huomaa että ollaan käytetty write() send() sijaan) # ts. read and write käännetään sisäisesti send ja recv kutsuiksi # tulostellaan kaikki mitä serveri oksentaa takaisin, koska meillä on vain request-toiminnallisuus #response = HttpResponse.read_from(f) #print response.status_code #print "**********************response body**********************" #print response.body response = HttpResponse(200,"OK","Begin body\n",headers={"Host":"localhost"}) response.write_to(sys.stdout) # for line in f: # print line # suljetaan file- sekä socket-objecti f.close() sukka.close()
def do_get(req: HttpRequest): paras = req.parameters action = paras.get('action', None) limit = paras.get('limit', 10) offset = paras.get('offset', 0) code = paras.get('code', None) login_name = paras.get('userInfo.login_name', None) if not login_name: user_info = paras.get('userInfo') if user_info: login_name = user_info.get('login_name', None) if isinstance(req, HttpRequest): req.res_head['Content-Type'] = 'application/json; charset=UTF-8' if action == 'get_image': b_ret = get_image(req=req, code=code) elif action == 'get_assets': b_ret = get_assets(req=req, code=code, login_name=login_name, limit=limit, offset=offset) else: req.res_command = ResponseCode.NOT_IMPLEMENTED package_body(req=req, status=Const.OpStatus.失败, op_type=Const.OpType.查询, message="不支持的动作") b_ret = False else: raise Exception('para req is not HttpRequest') return b_ret
def runserver(self): port = self.port routes = self.routes host = self.host with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind((host, port)) sock.listen() print(f"Listening on port {port}...") while True: conn, addr = sock.accept() # Receive request and parse to determine path request = HttpRequest(conn.recv(1024)) print(request.requestline) # Show the path accessed by the request path = request.path # Send an HTTP response on the socket if path in routes.keys(): conn.sendall(routes[path](request)) else: errormessage = f"You have accessed the following path: {path}" conn.sendall(filenotfound(errormessage)) conn.close() sock.close()
def _request_from_cffiprotocol(method: "char[]", path: "char[]", version: int, headers: "struct phr_header[]"): method = ffi.buffer(method)[:].decode('ascii') path = ffi.buffer(path)[:].decode('ascii') version = "1.0" if version == 0 else "1.1" headers = _extract_headers(headers) return HttpRequest(method, path, version, headers)
def recv_request(self, sock: socket): recv = sock.recv(4096) print(ServerMessage.CONNECTED.value) # 確認の為にRequestをファイルに書き出す with open("./resource/server/recv.txt", "wb") as f: f.writelines([recv]) return HttpRequest(recv)
def main(): host = "httpbin.org" sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((host, 80)) # https://tools.ietf.org/html/rfc7230#section-6.1 req = HttpRequest("GET", "/", headers={ "Host": host, "Connection": "close" }) f = sock.makefile() req.write_to(f) for line in f: print line f.close() sock.close()
def main(): host = "httpbin.org" s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, 80)) req = HttpRequest("GET", "/", headers={ "Host": host, "Connection": "close" }) f = s.makefile() req.write_to(f) for line in f: print line f.close() s.close()
def _request_from_cprotocol(method: memoryview, path: memoryview, version: int, headers: memoryview): method = method.tobytes().decode('ascii') path = path.tobytes().decode('ascii') version = "1.0" if version == 0 else "1.1" headers_len = headers.nbytes // ffi.sizeof("struct phr_header") headers_cdata = ffi.from_buffer(headers) headers_cdata = ffi.cast('struct phr_header[{}]'.format(headers_len), headers_cdata) headers = _extract_headers(headers_cdata) return HttpRequest(method, path, version, headers)
def main(self): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket: server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) server_socket.bind(("localhost", 8090)) server_socket.listen(1) print(ServerMessage.WAITING_CONNECTION.value) while True: client_socket, address = server_socket.accept() print(ServerMessage.CONNECTED.value) request = HttpRequest(client_socket.recv(4096)) with open("./resource/server/recv.txt", "wb") as f: f.write(request.as_bytes()) print(f"requested resouce is {DOCUMENT_ROOT + request.path}") try: with open(DOCUMENT_ROOT + request.path, "rb") as f: content = f.read() except FileNotFoundError: thread = HttpResponseThread(client_socket, HttpResponse404()) else: ext = request.path.split(".")[-1] if ext == "html": content_type = "text/html" elif ext == "css": content_type = "text/css" elif ext == "js": content_type = "text/javascript" thread = HttpResponseThread( client_socket, HttpResponse(content, content_type)) thread.start()
def do_post(req: HttpRequest): assets_impl = AssetsImpl(_db=req.my_db, _img_db=req.my_img_db) if isinstance(req, HttpRequest): req.res_head['Content-Type'] = 'application/json; charset=UTF-8' _user, _assets, reason, trace_id = get_post_data(req) _user = assets_impl.get_or_create_user( _user, trace_id) # 如果session有了,应该先从session里面获取 _assets.create_time, _assets.op_time = time.time(), time.time() _assets.status = Const.AssetsStatus.未借出.value _assets.user_id, _assets.user_name = _user.id, _user.name _assets.dst_user_id, _assets.dst_user_name, _assets.dst_user_mobile = _user.id, _user.name, _user.mobile status, _assets, op_type, message = assets_impl.create_assets( _assets=_assets, _user=_user, trace_id=trace_id) package_body(req=req, status=status, op_type=op_type, message=message, assets=_assets.to_html_dict()) else: raise Exception('para req is not HttpRequest') return True
def _parse_headers(self): self.num_headers[0] = 10 # FIXME: More than 10 headers result = lib.phr_parse_request(ffi.from_buffer(self.buffer), len(self.buffer), self.c_method, self.method_len, self.c_path, self.path_len, self.minor_version, self.c_headers, self.num_headers, 0) if result == -2: return result elif result == -1: self.on_error('malformed_headers') self._reset_state() self.buffer = bytearray() return result else: self._reset_state() method = ffi.string(self.c_method[0], self.method_len[0]).decode('ascii') path = ffi.string(self.c_path[0], self.path_len[0]).decode('ascii') version = "1." + str(self.minor_version[0]) headers = {} for idx in range(self.num_headers[0]): header = self.c_headers[idx] name = ffi.string(header.name, header.name_len).decode('ascii').title() value = ffi.string(header.value, header.value_len).decode('latin1') headers[name] = value self.buffer = self.buffer[result:] if self.minor_version[0] == 0: self.connection = headers.get('Connection', 'close') self.transfer = 'identity' else: self.connection = headers.get('Connection', 'keep-alive') self.transfer = headers.get('Transfer-Encoding', 'chunked') self.content_length = headers.get('Content-Length') if self.content_length is not None: content_length_error = False if not self.content_length: content_length_error = True if not content_length_error and self.content_length[0] in '+-': content_length_error = True if not content_length_error: try: self.content_length = int(self.content_length) except ValueError: content_length_error = True if content_length_error: self.on_error('invalid_headers') self._reset_state() self.buffer = bytearray() return -1 self.request = HttpRequest(method, path, version, headers) self.on_headers(self.request) return result
class OSDF(object): """ Communicates with an OSDF server's REST interface to facilitate several operations (node creation, deletion, queries, etc.) """ def __init__(self, server, username, password, port=8123, ssl=False): self._server = server self._port = port self._username = username self._password = password self._ssl = ssl self._set_request() def _set_request(self): self._request = HttpRequest(self._server, self._username, self._password, self._port, self._ssl) @property def server(self): return self._server @server.setter def server(self, server): self._server = server # Redefine the request object self._set_request() @property def port(self): return self._port @port.setter def port(self, port): self._port = port # Redefine the request object self._set_request() @property def username(self): return self._username @username.setter def username(self, username): self._username = username # Redefine the request object self._set_request() @property def password(self): return self._password @password.setter def password(self, password): self._password = password # Redefine the request object self._set_request() @property def ssl(self): return self._ssl @ssl.setter def ssl(self, ssl): if type(ssl) is not bool: raise ValueError("Invalid value for ssl.") self._ssl = ssl # Redefine the request object self._set_request() def edit_node(self, json_data): """ Updates a node with the provided data """ # Get the node id from json_data if 'id' not in json_data: raise Exception("No node id in the provided JSON.") node_id = json_data['id'] json_str = json.dumps(json_data); osdf_response = self._request.put("/nodes/" + node_id, json_str) if osdf_response["code"] != 200: headers = osdf_response['headers'] self.header_error(headers, 'edit', 'node') def _byteify(self, input): if isinstance(input, dict): return {self._byteify(key):self._byteify(value) for key,value in input.iteritems()} elif isinstance(input, list): return [self._byteify(element) for element in input] elif isinstance(input, unicode): return input.encode('utf-8') else: return input def get_info(self): """ Retrieve's the OSDF server's information/contact document """ osdf_response = self._request.get("/info") info = json.loads( osdf_response['content'] ) info = self._byteify(info) return info def get_node(self, node_id): """ Retrieves an OSDF node given the node's ID Returns the parsed form of the JSON document for the node """ osdf_response = self._request.get("/nodes/" + node_id) if osdf_response["code"] != 200: headers = osdf_response['headers'] self.header_error(headers, 'retrieve', 'node') data = json.loads( osdf_response['content'] ) data = self._byteify(data) return data def get_nodes_in(self, node_id): """ Retrieves the nodes that link to the OSDF node identified by the given the node ID. """ url = "/nodes/{}/in".format(node_id) osdf_response = self._request.get(url) if osdf_response["code"] != 200: headers = osdf_response['headers'] self.header_error(headers, 'retrieve', 'node') data = json.loads( osdf_response['content'] ) data = self._byteify(data) return data def get_nodes_out(self, node_id): """ Retrieves the OSDF nodes that the given node links to (via it's linkage field). """ url = "/nodes/{}/out".format(node_id) osdf_response = self._request.get(url) if osdf_response["code"] != 200: headers = osdf_response['headers'] self.header_error(headers, 'retrieve', 'node') data = json.loads( osdf_response['content'] ) data = self._byteify(data) return data def get_node_by_version(self, node_id, version): """ Given a numerical version number, retrieves an OSDF node's data as it was at that version. Returns the parsed form of the JSON document for the node """ osdf_response = self._request.get("/nodes/%s/ver/%s" % (node_id, version)) if osdf_response["code"] != 200: headers = osdf_response['headers'] self.header_error(headers, 'retrieve', 'node') data = json.loads( osdf_response['content'] ) data = self._byteify(data) return data def get_schemas(self, namespace): """ Retrieves all of the schemas for a particular namespace. """ url = '/namespaces/%s/schemas/' % namespace osdf_response = self._request.get(url) if osdf_response["code"] != 200: headers = osdf_response['headers'] self.header_error(headers, 'retrieve', 'schemas') all_schema_data = json.loads( osdf_response['content'] ) schema_data = self._byteify(all_schema_data) return all_schema_data def get_schema(self, namespace, schema_name): """ Retrieves a namespace's document schema Returns the parsed form of the JSON-Schema document """ url = '/namespaces/%s/schemas/%s' % (namespace, schema_name) osdf_response = self._request.get(url) if osdf_response["code"] != 200: headers = osdf_response['headers'] self.header_error(headers, 'retrieve', 'schema') schema_data = json.loads( osdf_response['content'] ) schema_data = self._byteify(schema_data) return schema_data def get_aux_schemas(self, namespace): """ Retrieves all of the auxiliary schemas for a particular namespace. Returns the parsed form of the auxiliary schemas. """ url = '/namespaces/%s/schemas/aux/' % (namespace) osdf_response = self._request.get(url) if osdf_response["code"] != 200: headers = osdf_response['headers'] self.header_error(headers, 'retrieve', 'aux schemas') aux_schema_data = json.loads( osdf_response['content'] ) aux_schema_data = self._byteify(aux_schema_data) return aux_schema_data def get_aux_schema(self, namespace, aux_schema_name): """ Retrieves an auxiliary schema Returns the parsed form of the auxiliary schema JSON """ url = '/namespaces/%s/schemas/aux/%s' % (namespace, aux_schema_name) osdf_response = self._request.get(url) if osdf_response["code"] != 200: headers = osdf_response['headers'] self.header_error(headers, 'retrieve', 'aux schema') aux_schema_data = json.loads( osdf_response['content'] ) aux_schema_data = self._byteify(aux_schema_data) return aux_schema_data def insert_node(self, json_data): """ Inserts a node with the provided data into OSDF Returns the node ID upon successful insertion. """ json_str = json.dumps(json_data); osdf_response = self._request.post("/nodes", json_str) node_id = None headers = osdf_response["headers"] if osdf_response["code"] == 201: if 'location' in headers: node_id = headers['location'].split('/')[-1] else: raise Exception("No location header for the newly inserted node.") else: if 'x-osdf-error' in headers: msg = "Unable to insert node document. Reason: " + headers['x-osdf-error'] else: msg = "Unable to insert node document." raise Exception(msg) return node_id def delete_node(self, node_id): """ Deletes the specified node from OSDF. """ osdf_response = self._request.delete("/nodes/" + node_id) if osdf_response['code'] != 204: headers = osdf_response['headers'] self.header_error(headers, 'delete', 'node') def validate_node(self, json_data): """ Report whether a node document validates against OSDF and its notion of what that node should look like according to any registered schemas. Returns a tuple with the first value holding a boolean of whether the document validated or not. The second value contains the error message if the document did not validate. """ json_str = json.dumps(json_data); url = "/nodes/validate" osdf_response = self._request.post(url, json_str) headers = osdf_response["headers"] valid = False error_msg = None if osdf_response["code"] != 200: if 'x-osdf-error' in headers: error_msg = headers['x-osdf-error'] else: error_msg = "Unknown" else: valid = True return (valid, error_msg) def oql_query(self, namespace, query, page=1): """ Issue an OSDF Query Language (OQL) query against OSDF. Returns the specified page of results. """ url = "/nodes/oql/%s/page/%s" % (namespace, str(page)) osdf_response = self._request.post(url, query) if osdf_response["code"] != 200 and osdf_response["code"] != 206: headers = osdf_response["headers"] if 'x-osdf-error' in headers: msg = "Unable to query namespace %s. Reason: %s" % (namespace, headers['x-osdf-error']) else: msg = "Unable to query namespace." raise Exception(msg) data = json.loads( osdf_response['content'] ) data = self._byteify(data) return data def query(self, namespace, query, page=1): """ Issue a query against OSDF. Queries are expressed in JSON form using the ElasticSearch Query DSL. Returns the specified page of results. """ url = "/nodes/query/%s/page/%s" % (namespace, str(page)) osdf_response = self._request.post(url, query) if osdf_response["code"] != 200 and osdf_response["code"] != 206: headers = osdf_response["headers"] if 'x-osdf-error' in headers: msg = "Unable to query namespace %s. Reason: %s" % (namespace, headers['x-osdf-error']) else: msg = "Unable to query namespace." raise Exception(msg) data = json.loads( osdf_response['content'] ) data = self._byteify(data) return data def oql_query_all_pages(self, namespace, query): """ Issue an OSDF Query Language (OQL) query against OSDF, as in the oql_query() method, but retrieves ALL results by aggregating all the available pages of results. Use with caution, as this may consume a lot of memory with large result sets. """ more_results = True page = 1 cumulative_results = [] while more_results: results = self.oql_query(namespace, query, page) cumulative_results.extend(results['results']) if results['result_count'] > 0: page += 1 else: more_results = False results['results'] = cumulative_results results['result_count'] = len(results['results']) del results['page'] return results; def query_all_pages(self, namespace, query): """ Issue a query against OSDF, as in the query() method, but retrieves ALL results by aggregating all the available pages of results. Use with caution, as this may consume a lot of memory with large result sets. """ more_results = True page = 1 cumulative_results = [] while more_results: results = self.query(namespace, query, page) cumulative_results.extend(results['results']) if results['result_count'] > 0: page += 1 else: more_results = False results['results'] = cumulative_results results['result_count'] = len(results['results']) del results['page'] return results; def create_osdf_node(self, namespace, node_type, domain_json, linkage={}, read="all", write="all"): node_json = { 'ns': namespace, 'acl': { 'read': [ read ], 'write': [ write ] }, 'linkage': linkage, 'meta': domain_json, 'node_type': node_type } return node_json def header_error(self, headers=[], method_type='retrieve', document_type=None): if 'x-osdf-error' in headers: msg = "Unable to %s %s document. Reason: %s" \ % (method_type, document_type, headers['x-osdf-error']) else: msg = "Unable to %s %s document." \ % (method_type, document_type) raise Exception(msg)
def _set_request(self): self._request = HttpRequest(self._server, self._username, self._password, self._port, self._ssl)
def port(self, port): self._port = port # Redefine the request object self._request = HttpRequest(self._server, self._username, self._password, self._port)
class OSDF: """ Communicates with an OSDF server's REST interface to facilitate several operations (node creation, deletion, queries, etc.) """ def __init__(self, server, username, password, port=8123): self.server = server self.port = port self.username = username self.password = password self._request = HttpRequest(server, username, password, port=port) def edit_node(self, json_data): """ Updates a node with the provided data """ # Get the node id from json_data if 'id' not in json_data: raise Exception("No node id in the provided JSON.") node_id = json_data['id'] json_str = json.dumps(json_data); osdf_response = self._request.put("/nodes/" + node_id, json_str) if osdf_response["code"] != 200: headers = osdf_response['headers'] if 'x-osdf-error' in headers: msg = "Unable to edit node document. Reason: " \ + headers['x-osdf-error'] raise Exception(msg) else: raise Exception("Unable to edit node document.") def get_info(self): """ Retrieve's the OSDF server's information/contact document """ osdf_response = self._request.get("/info") info = json.loads( osdf_response['content'] ) return info def get_node(self, node_id): """ Retrieves an OSDF node given the node's ID Returns the parsed form of the JSON document for the node """ osdf_response = self._request.get("/nodes/" + node_id) if osdf_response["code"] != 200: headers = osdf_response['headers'] if 'x-osdf-error' in headers: msg = "Unable to retrieve node document. Reason: " \ + headers['x-osdf-error'] else: msg = "Unable to retrieve node document." raise Exception(msg) data = json.loads( osdf_response['content'] ) return data def get_schema(self, namespace, schema_name): """ Retrieves a namespace's document schema Returns the parsed form of the JSON-Schema document """ url = '/namespaces/%s/schemas/%s' % (namespace, schema_name) osdf_response = self._request.get(url) if osdf_response["code"] != 200: headers = osdf_response['headers'] if 'x-osdf-error' in headers: msg = "Unable to retrieve schema document. Reason: " \ + headers['x-osdf-error'] else: msg = "Unable to retrieve schema document." raise Exception(msg) schema_data = json.loads( osdf_response['content'] ) return schema_data def get_aux_schema(self, namespace, aux_schema_name): """ Retrieves an auxiliary schema Returns the parsed form of the auxiliary schema JSON """ url = '/namespaces/%s/schemas/aux/%s' % (namespace, aux_schema_name) osdf_response = self._request.get(url) if osdf_response["code"] != 200: headers = osdf_response['headers'] if 'x-osdf-error' in headers: msg = "Unable to retrieve schema document. Reason: " \ + headers['x-osdf-error'] else: msg = "Unable to retrieve schema document." raise Exception(msg) aux_schema_data = json.loads( osdf_response['content'] ) return aux_schema_data def insert_node(self, json_data): """ Inserts a node with the provided data into OSDF Returns the node ID upon successful insertion. """ json_str = json.dumps(json_data); osdf_response = self._request.post("/nodes", json_str) node_id = None headers = osdf_response["headers"] if osdf_response["code"] == 201: if 'location' in headers: node_id = headers['location'].split('/')[-1] else: raise Exception("No location header for the newly inserted node.") else: if 'x-osdf-error' in headers: msg = "Unable to insert node document. Reason: " + headers['x-osdf-error'] else: msg = "Unable to insert node document." raise Exception(msg) return node_id def delete_node(self, node_id): """ Deletes the specified node from OSDF. """ osdf_response = self._request.delete("/nodes/" + node_id) if osdf_response['code'] != 200: if 'x-osdf-error' in headers: msg = "Unable to delete node document. Reason: " + headers['x-osdf-error'] else: msg = "Unable to delete node document." raise Exception(msg) def validate_node(self, json_data): """ Report whether a node document validates against OSDF and it's notion of what that node should look like according to any registered schemas. Returns a tuple with the first value holding a boolean of whether the document validated or not. The second value contains the error message if the document did not validate. """ json_str = json.dumps(json_data); url = "/nodes/validate" osdf_response = self._request.post(url, json_str) headers = osdf_response["headers"] valid = False error_msg = None if osdf_response["code"] != 200: if 'x-osdf-error' in headers: error_msg = headers['x-osdf-error'] else: error_msg = "Unknown" else: valid = True return (valid, error_msg) def query(self, namespace, query, page=1): """ Issue a query against OSDF. Queries are expressed in JSON form using the ElasticSearch Query DSL. Returns the specified page of results. """ url = "/nodes/query/%s/page/%s" % (namespace, str(page)) osdf_response = self._request.post(url, query) if osdf_response["code"] != 200 and osdf_response["code"] != 206: headers = osdf_response["headers"] if 'x-osdf-error' in headers: msg = "Unable to query namespace %s. Reason: %s" % (namespace, headers['x-osdf-error']) else: msg = "Unable to query namespace." raise Exception(msg) data = json.loads( osdf_response['content'] ) return data def query_all_pages(self, namespace, query): """ Issue a query against OSDF, as in the query() method, but retrieves ALL results by aggregating all the available pages of results. Use with caution, as this may consume a lot of memory with large result sets. """ more_results = True page = 1 cumulative_results = [] while more_results: results = self.query(namespace, query, page) cumulative_results.extend(results['results']) if results['result_count'] > 0: page += 1 else: more_results = False results['results'] = cumulative_results results['result_count'] = len(results['results']) del results['page'] return results; def create_osdf_node(self, namespace, node_type, domain_json, linkage={}, read="all", write="all"): node_json = { 'ns': namespace, 'acl': { 'read': [ read ], 'write': [ write ] }, 'linkage': linkage, 'meta': domain_json, 'node_type': node_type } return node_json
def __init__(self, server, username, password, port=8123): self.server = server self.port = port self.username = username self.password = password self._request = HttpRequest(server, username, password, port=port)
def tcp_link(sock, addr, alive_sock_queue, _db, _img_db): t_start = time.time() my_print('Accept new connection from {0}...'.format(addr, )) exception_trace_id = generate_trace_id() exception_message = None has_exception = True req = HttpRequest(sock, addr, _db=_db, _img_db=_img_db, trace_id=exception_trace_id) need_send = False try: success = req.do() if success: # 成功的请求,以后可以缓存 pass has_exception = False need_send = True except Exception as e: tb = traceback.format_exc() if str(e).find('client socket closed.') != -1: return exception_message = str(e) except BaseException as be: tb = traceback.format_exc() exception_message = str(be) if has_exception: my_print('tcp_link TraceId:{0} {1} {2}'.format(exception_trace_id, exception_message, tb)) req.res_command = ResponseCode.INNER_ERROR body = dict() body['status'] = 9999 body['message'] = exception_message body['traceId'] = exception_trace_id ymp = json.dumps(body, ensure_ascii=False) req.res_body = ymp.encode('UTF-8') req.res_head['Content-Type'] = 'text/html; charset=UTF-8' need_send = True if need_send: if req.res_body is None or 0 == len(req.res_body): my_print("trace_id:{} header:{} body:{}".format( req.trace_id, req._raw_head, get_print_string(req._raw_body))) req.res_body = 'UNKNOWN ERROR,trace_id:{} req.trace_id:{}'.format( req.trace_id, req.parameters.get('trace_id', None)).encode('utf-8') req.res_command = b"HTTP/1.1 500 OK\r\n" req.res_head['Content-Length'] = str(len(req.res_body)) response_header = dict2header(req.res_head).encode('UTF-8') if Conf.get('is_save_response'): write_to_file(Conf.get('save_response_file_path'), req.get_res_data()) sock.sendall(req.res_command) sock.sendall(response_header) sock.sendall(req.res_body) if req.res_head.get('Connection', 'Close').lower() == 'keep-alive': data = types.SimpleNamespace(sock=sock, addr=addr) alive_sock_queue.put(data) else: sock.close() else: sock.close() t_stop = time.time() print( "[%d] %s,%s %s %.5f" % (os.getpid(), addr[0], addr[1], req.command.command, t_stop - t_start))
def username(self, username): self._username = username # Redefine the request object self._request = HttpRequest(self._server, self._username, self._password, self._port)
def server(self, server): self._server = server # Redefine the request object self._request = HttpRequest(self._server, self._username, self._password, self._port)
def password(self, password): self._password = password # Redefine the request object self._request = HttpRequest(self._server, self._username, self._password, self._port)