From 46c6e5c2aed587ba7d257149b6ff2616e63e48b3 Mon Sep 17 00:00:00 2001 From: RustamOper05 Date: Wed, 17 Jan 2024 21:20:18 +0300 Subject: [PATCH] add get req --- backend/backend.py | 149 +++++++++++++++++++++++++++++++++++------- backend/edit_route.py | 8 +++ backend/req_test.py | 9 ++- backend/route.json | 2 +- 4 files changed, 144 insertions(+), 24 deletions(-) diff --git a/backend/backend.py b/backend/backend.py index 1d573a4..81afdd2 100644 --- a/backend/backend.py +++ b/backend/backend.py @@ -3,19 +3,108 @@ import socketserver import subprocess from urllib.parse import urlparse, parse_qs import base64 +import json +import re class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler): def do_GET(self): + + with open("./route.json", "r") as f: + route_dict = json.load(f) + + with open("suka_logs.log", "w") as f: + f.write("starting...") + + user_hash, post_hash, comment_hash = compile_and_deploy_contract( + "../User", + "../Post", + "../Comment", + "one" + ) + + with open("suka_logs.log", "a") as f: + f.write(f"user_hash: {user_hash}\npost_hash: {post_hash}\ncomment_hash: {comment_hash}") + parsed_path = urlparse(self.path) query = parse_qs(parsed_path.query) - print(query) - if "command" in query and "args" in query: + if "command_type" in query and "command_id" in query: self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() - self.wfile.write(bytes(f"command: {query['command']}, args: {query['args']}", "utf-8")) + + params = query.get("args", None) + parsed_params = " " + ' '.join(params) if params else "" + + command_type = query["command_type"][0] + contract_name = query["contract"][0] + method = query["method"][0] + + with open("suka_logs.log", "a") as f: + f.write("reading params...\n") + + if command_type == "get": + hash_for_req = None + if contract_name == "post": + hash_for_req = post_hash + elif contract_name == "user": + hash_for_req = user_hash + elif contract_name == "comment": + hash_for_req = comment_hash + + with open("suka_logs.log", "a") as f: + f.write(f"params: {[command_type, contract_name, method, parsed_params]}\n") + + command = f"neo-go contract testinvokefunction -r http://localhost:30333 {hash_for_req} {method}{parsed_params}" + + proc = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) + + out_list = [] + while True: + if proc.stdout is None: + break + + line = proc.stdout.readline() + + if not line: + break + out_list.append(line.rstrip()) + + answer = " ".join(map(lambda x: x.decode("utf-8"), out_list)) + answer = re.sub(r"[\n\t\s]*", "", answer) + + answer_dict = json.loads(answer)["stack"][0]["value"] + + answer_list = [] + for obj_i in answer_dict: + obj = obj_i["value"] + obj_fiedls = [] + for field in obj: + if field["type"] in ["ByteString", "Buffer"]: + encoded_string = field["value"] + result_string = base64.b64decode(encoded_string) + result_string = result_string.decode('utf-8') + obj_fiedls.append(result_string) + elif field["type"] == "Integer": + encoded_string = field["value"] + obj_fiedls.append(encoded_string) + + answer_list.append(obj_fiedls) + + self.wfile.write(bytes(f"answer: {obj_fiedls}", "utf-8")) + + + # elif command_type == "post": + # command = commands_route["7"]["command"] + f"-r http://localhost:30333 {contract_name} {method} {' '.join(params[5:])}" + + + # if command_type == "post": + # command = commands_route[command_code] + # else: + + # user_deploy_command = f"sshpass -p {password} neo-go contract deploy -i {user_path}/user.nef -w /home/ftmi-krasotka/Desktop/frostfs_test/frostfs-aio/morph/node-wallet.json -r http://localhost:30333 -m {user_path}/user.json --force" + # proc = subprocess.Popen(user_deploy_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) else: self.send_response(400) @@ -31,10 +120,8 @@ def compile_and_deploy_contract( password: str, ) -> dict[str, str]: - # User user_compile_command = f"sshpass -p {password} neo-go contract compile -i {user_path}/user.go -c {user_path}/user.yml -m {user_path}/user.json" subprocess.run(user_compile_command, shell=True, check=False) - user_deploy_command = f"sshpass -p {password} neo-go contract deploy -i {user_path}/user.nef -w /home/ftmi-krasotka/Desktop/frostfs_test/frostfs-aio/morph/node-wallet.json -r http://localhost:30333 -m {user_path}/user.json --force" proc = subprocess.Popen(user_deploy_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) @@ -45,13 +132,10 @@ def compile_and_deploy_contract( break user_out_list.append(line.rstrip()) - user_hash = str(user_out_list[-1]).replace("Contract: ", "") - print(user_hash) + user_hash = str(user_out_list[-1], encoding='utf-8').replace("Contract: ", "") - # Post - post_compile_command = f"sshpass -p {password} neo-go contract compile -i {post_path}/post_contract.go -c {post_path}/post_contract.yml -m {post_path}//post_contract.json" + post_compile_command = f"sshpass -p {password} neo-go contract compile -i {post_path}/post_contract.go -c {post_path}/post_contract.yml -m {post_path}/post_contract.json" subprocess.run(post_compile_command, shell=True, check=False) - post_deploy_command = f"sshpass -p {password} neo-go contract deploy -i {post_path}/post_contract.nef -w /home/ftmi-krasotka/Desktop/frostfs_test/frostfs-aio/morph/node-wallet.json -r http://localhost:30333 -m {post_path}/post_contract.json --force" proc = subprocess.Popen(post_deploy_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) @@ -62,13 +146,10 @@ def compile_and_deploy_contract( break post_out_list.append(line.rstrip()) - post_hash = str(post_out_list[-1]).replace("Contract: ", "") - print(post_hash) + post_hash = str(post_out_list[-1], encoding='utf-8').replace("Contract: ", "") - # Comment comment_compile_command = f"sshpass -p {password} neo-go contract compile -i {comment_path}/comment.go -c {comment_path}/comment.yml -m {comment_path}/comment.json" subprocess.run(comment_compile_command, shell=True, check=False) - comment_deploy_command = f"sshpass -p {password} neo-go contract deploy -i {comment_path}/comment.nef -w /home/ftmi-krasotka/Desktop/frostfs_test/frostfs-aio/morph/node-wallet.json -r http://localhost:30333 -m {comment_path}/comment.json --force" proc = subprocess.Popen(comment_deploy_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) @@ -79,17 +160,41 @@ def compile_and_deploy_contract( break comment_out_list.append(line.rstrip()) - comment_hash = str(comment_out_list[-1]).replace("Contract: ", "") - print(comment_hash) + comment_hash = str(comment_out_list[-1], encoding='utf-8').replace("Contract: ", "") + + print(user_hash, post_hash, comment_hash) + + ######################################## + comment_deploy_command = f"sshpass -p {password} neo-go contract invokefunction -w /home/ftmi-krasotka/Desktop/frostfs_test/frostfs-aio/morph/node-wallet.json -r http://localhost:30333 {user_hash} newUser hui228336 hui228336 hui228336 1 Nhfg3TbpwogLvDGVvAvqyThbsHgoSUKwtn" + proc = subprocess.Popen(comment_deploy_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) + + comment_deploy_command = f"sshpass -p {password} neo-go contract invokefunction -w /home/ftmi-krasotka/Desktop/frostfs_test/frostfs-aio/morph/node-wallet.json -r http://localhost:30333 {user_hash} newUser hui1488 hui1488 hui1488 1 Nhfg3TbpwogLvDGVvAvqyThbsHgoSUKwtn" + proc = subprocess.Popen(comment_deploy_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) + + comment_deploy_command = f"sshpass -p {password} neo-go contract invokefunction -w /home/ftmi-krasotka/Desktop/frostfs_test/frostfs-aio/morph/node-wallet.json -r http://localhost:30333 {post_hash} newPost hui228336 hui hui" + proc = subprocess.Popen(comment_deploy_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) + + comment_deploy_command = f"sshpass -p {password} neo-go contract invokefunction -w /home/ftmi-krasotka/Desktop/frostfs_test/frostfs-aio/morph/node-wallet.json -r http://localhost:30333 {post_hash} newPost hui228336 hui22 hui22" + proc = subprocess.Popen(comment_deploy_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) + + comment_deploy_command = f"sshpass -p {password} neo-go contract invokefunction -w /home/ftmi-krasotka/Desktop/frostfs_test/frostfs-aio/morph/node-wallet.json -r http://localhost:30333 {comment_hash} createNewComment hui228336 post_1 some_text" + proc = subprocess.Popen(comment_deploy_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) + + comment_deploy_command = f"sshpass -p {password} neo-go contract invokefunction -w /home/ftmi-krasotka/Desktop/frostfs_test/frostfs-aio/morph/node-wallet.json -r http://localhost:30333 {comment_hash} createNewComment hui1488 post_1 some_text" + proc = subprocess.Popen(comment_deploy_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) + + comment_deploy_command = f"sshpass -p {password} neo-go contract invokefunction -w /home/ftmi-krasotka/Desktop/frostfs_test/frostfs-aio/morph/node-wallet.json -r http://localhost:30333 {comment_hash} createNewComment hui1488 post_1 some_text" + proc = subprocess.Popen(comment_deploy_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) + ######################################## + + return user_hash, post_hash, comment_hash if __name__ == "__main__": - compile_and_deploy_contract("../User", "../Post", "../Comment", "one") - PORT = 2281 Handler = MyHttpRequestHandler - # with socketserver.TCPServer(("", PORT), Handler) as httpd: - # print("Сервер запущен на порту", PORT) - # httpd.serve_forever() - # httpd.server_close() + with socketserver.TCPServer(("", PORT), Handler) as httpd: + print("Сервер запущен на порту", PORT) + httpd.serve_forever() + httpd.server_close() diff --git a/backend/edit_route.py b/backend/edit_route.py index d3c82e0..a1109ca 100644 --- a/backend/edit_route.py +++ b/backend/edit_route.py @@ -55,6 +55,14 @@ route = { "func_name": "name of invoked func", "parameters": "list([]) of func parameters" } + }, + "8": { + "command": "neo-go contract testinvokefunction", + "flags": { + "contract_hash": "hash of contract", + "func_name": "name of invoked func", + "parameters": "list([]) of func parameters" + } } } diff --git a/backend/req_test.py b/backend/req_test.py index e190931..11a6324 100644 --- a/backend/req_test.py +++ b/backend/req_test.py @@ -1,7 +1,14 @@ import requests # Отправляем GET запрос на сервер -response = requests.get('http://localhost:2281', params={"command": 0, "args": {"amount_1": 1, "amount": 1}}) +response = requests.get( + 'http://localhost:2281', + params={ + "command_type": "get", + "method": "getAllPosts", + "command_id": "8", + "contract": "post", + "args": []}) # Выводим ответ от сервера print('Ответ от сервера:', response.text) diff --git a/backend/route.json b/backend/route.json index 80555fa..2f94cc0 100644 --- a/backend/route.json +++ b/backend/route.json @@ -1 +1 @@ -{"1": {"command": "neo-go wallet init", "flags": {"wallet": "wallet file name", "account": "create new account"}}, "2": {"command": "neo-go wallet dump-keys", "flags": {"wallet": "wallet file name"}}, "3": {"command": "neo-go wallet nep17 balance", "flags": {"wallet": "wallet file name"}}, "4": {"command": "neo-go wallet nep17 transfer", "flags": {"wallet": "wallet file name(wallet from which transfer is made)", "from": "hash of wallet from which transfer is made", "to": "hash of wallet to which transfer is made", "amount": "gas transfer amount", "token": "token for transfer NEO/GAS"}}, "5": {"command": "neo-go contract compile", "flags": {"in": "smart contract file name(*.go)", "manifest": "contract manifest file name(*.json)", "contract_config": "contract config file name(*.yml)"}}, "6": {"command": "neo-go contract deploy", "flags": {"wallet": "wallet file name", "in": "compiled smart contract file name(*.nef)", "manifest": "manifest file name(*.json)"}}, "7": {"command": "neo-go contract invokefunction", "flags": {"wallet": "wallet file name", "contract_hash": "hash of contract", "func_name": "name of invoked func", "parameters": "list([]) of func parameters"}}} \ No newline at end of file +{"1": {"command": "neo-go wallet init", "flags": {"wallet": "wallet file name", "account": "create new account"}}, "2": {"command": "neo-go wallet dump-keys", "flags": {"wallet": "wallet file name"}}, "3": {"command": "neo-go wallet nep17 balance", "flags": {"wallet": "wallet file name"}}, "4": {"command": "neo-go wallet nep17 transfer", "flags": {"wallet": "wallet file name(wallet from which transfer is made)", "from": "hash of wallet from which transfer is made", "to": "hash of wallet to which transfer is made", "amount": "gas transfer amount", "token": "token for transfer NEO/GAS"}}, "5": {"command": "neo-go contract compile", "flags": {"in": "smart contract file name(*.go)", "manifest": "contract manifest file name(*.json)", "contract_config": "contract config file name(*.yml)"}}, "6": {"command": "neo-go contract deploy", "flags": {"wallet": "wallet file name", "in": "compiled smart contract file name(*.nef)", "manifest": "manifest file name(*.json)"}}, "7": {"command": "neo-go contract invokefunction", "flags": {"wallet": "wallet file name", "contract_hash": "hash of contract", "func_name": "name of invoked func", "parameters": "list([]) of func parameters"}}, "8": {"command": "neo-go contract testinvokefunction", "flags": {"contract_hash": "hash of contract", "func_name": "name of invoked func", "parameters": "list([]) of func parameters"}}} \ No newline at end of file