From f7f016789ff06bd1fc77e9acd75cef68a03d6c42 Mon Sep 17 00:00:00 2001 From: Moul <moul@moul.re> Date: Wed, 15 Jan 2020 07:55:17 +0100 Subject: [PATCH] [enh] #58: WS2P examples improvements: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Request blocks around 30.000 as 360.000 does not already exists in Äž1 - Move dummy credential before getpass in order to be overwriten - Add ability to pass member credentials to listen_ws2p - Add getpass import - Close WS2P clients in three examples in case of a raised exception - Use sys.exit() because pylint complains --- examples/listen_ws2p.py | 13 ++++++++++++- examples/request_web_socket_block.py | 1 + examples/request_ws2p.py | 19 +++++++++++-------- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/examples/listen_ws2p.py b/examples/listen_ws2p.py index 5e2884cc..1abb60cc 100644 --- a/examples/listen_ws2p.py +++ b/examples/listen_ws2p.py @@ -1,5 +1,6 @@ import asyncio import json +import sys from _socket import gaierror @@ -31,6 +32,15 @@ async def main(): # Arbitrary credentials to create the node key pair to sign ws2p documents salt = password = "test" + # You can connect with member credentials in case there is not much slots available on the endpoint + # + # # Prompt hidden user entry + # import getpass + # salt = getpass.getpass("Enter your passphrase (salt): ") + # + # # Prompt hidden user entry + # password = getpass.getpass("Enter your password: ") + # Init signing_key instance signing_key = SigningKey.from_credentials(salt, password) @@ -56,7 +66,7 @@ async def main(): except ValidationError as exception: print(exception.message) print("HANDSHAKE FAILED !") - exit(1) + sys.exit(1) print("Handshake ok") @@ -78,6 +88,7 @@ async def main(): print("{0} : {1}".format(str(e), ws2p_endpoint.inline())) except jsonschema.ValidationError as e: print("{:}:{:}".format(str(e.__class__.__name__), str(e))) + await client.close() # Latest duniter-python-api is asynchronous and you have to use asyncio, an asyncio loop and a "as" on the data. diff --git a/examples/request_web_socket_block.py b/examples/request_web_socket_block.py index 16ee1b72..d0ebfa24 100644 --- a/examples/request_web_socket_block.py +++ b/examples/request_web_socket_block.py @@ -51,6 +51,7 @@ async def main(): print("{0} : {1}".format(str(e), BMAS_ENDPOINT)) except jsonschema.ValidationError as e: print("{:}:{:}".format(str(e.__class__.__name__), str(e))) + await client.close() # Latest duniter-python-api is asynchronous and you have to use asyncio, an asyncio loop and a "as" on the data. diff --git a/examples/request_ws2p.py b/examples/request_ws2p.py index 545a1951..9edc1248 100644 --- a/examples/request_ws2p.py +++ b/examples/request_ws2p.py @@ -1,6 +1,7 @@ import asyncio import json import time +import sys from _socket import gaierror @@ -69,17 +70,18 @@ async def main(): """ Main code """ + # dummy credentials + salt = password = "test" + # You can connect with member credentials in case there is not much slots available on the endpoint # # # Prompt hidden user entry + # import getpass # salt = getpass.getpass("Enter your passphrase (salt): ") # # # Prompt hidden user entry # password = getpass.getpass("Enter your password: ") - # dummy credentials - salt = password = "test" - # Init signing_key instance signing_key = SigningKey.from_credentials(salt, password) @@ -103,7 +105,7 @@ async def main(): except ValidationError as exception: print(exception.message) print("HANDSHAKE FAILED !") - exit(1) + sys.exit(1) # Send ws2p request print("Send getCurrent() request") @@ -117,22 +119,22 @@ async def main(): print("Response: " + json.dumps(response, indent=2)) # Send ws2p request - print("Send getBlock(360000) request") + print("Send getBlock(30000) request") request_id = get_ws2p_challenge()[:8] response = await send( ws, - requests.get_block(request_id, 360000), + requests.get_block(request_id, 30000), request_id, requests.BLOCK_RESPONSE_SCHEMA, ) print("Response: " + json.dumps(response, indent=2)) # Send ws2p request - print("Send getBlocks(360000, 2) request") + print("Send getBlocks(30000, 2) request") request_id = get_ws2p_challenge()[:8] response = await send( ws, - requests.get_blocks(request_id, 360000, 2), + requests.get_blocks(request_id, 30000, 2), request_id, requests.BLOCKS_RESPONSE_SCHEMA, ) @@ -158,6 +160,7 @@ async def main(): print("{0} : {1}".format(str(e), ws2p_endpoint.inline())) except jsonschema.ValidationError as e: print("{:}:{:}".format(str(e.__class__.__name__), str(e))) + await client.close() # Latest duniter-python-api is asynchronous and you have to use asyncio, an asyncio loop and a "as" on the data. -- GitLab