Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
DuniterPy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
clients
python
DuniterPy
Commits
4805d0b6
Commit
4805d0b6
authored
5 years ago
by
Vincent Texier
Browse files
Options
Downloads
Patches
Plain Diff
[enh]
#58
improve ws2p example
parent
f582ecd7
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/request_ws2p.py
+53
-44
53 additions, 44 deletions
examples/request_ws2p.py
with
53 additions
and
44 deletions
examples/request_ws2p.py
+
53
−
44
View file @
4805d0b6
...
...
@@ -33,14 +33,14 @@ async def main():
Main code
"""
# #
p
rompt hidden user entry
# #
P
rompt hidden user entry
# salt = getpass.getpass("Enter your passphrase (salt): ")
#
# #
p
rompt hidden user entry
# #
P
rompt hidden user entry
# password = getpass.getpass("Enter your password: ")
salt
=
password
=
"
toto
"
#
i
nit signing_key instance
#
I
nit signing_key instance
signing_key
=
SigningKey
.
from_credentials
(
salt
,
password
)
connect_document
=
Connect
(
CURRENCY
,
signing_key
.
pubkey
)
...
...
@@ -74,7 +74,11 @@ async def main():
# Iterate on each message received...
async
for
msg
in
ws
:
# if message type is text...
# Display incoming message from peer
print
(
msg
)
# If message type is text...
if
msg
.
type
==
aiohttp
.
WSMsgType
.
TEXT
:
# print(msg.data)
try
:
...
...
@@ -98,19 +102,27 @@ async def main():
Ok
(
CURRENCY
,
remote_connect_document
.
pubkey
,
connect_document
.
challenge
,
data
[
"
sig
"
])
print
(
"
Received OK message signature is valid
"
)
# do not wait for messages anymore
# END HANDSHAKE #######################################################
print
(
"
END OF HANDSHAKE
\n
"
)
# Uncomment the following command to stop listening for messages anymore
break
# Uncomment the following commands to continue to listen incoming messages
# print("waiting for incoming messages...\n")
# continue
print
(
"
Received a ACK message
"
)
#
c
reate ACK document from ACK response to verify signature
#
C
reate ACK document from ACK response to verify signature
Ack
(
CURRENCY
,
data
[
"
pub
"
],
connect_document
.
challenge
,
data
[
"
sig
"
])
print
(
"
Received ACK message signature is valid
"
)
#
Si
ACK response ok, create OK message
#
If
ACK response
is
ok, create OK message
ok_message
=
Ok
(
CURRENCY
,
signing_key
.
pubkey
,
connect_document
.
challenge
).
get_signed_json
(
signing_key
)
#
s
end OK message
#
S
end OK message
print
(
"
Send OK message...
"
)
await
ws
.
send_str
(
ok_message
)
continue
...
...
@@ -123,7 +135,7 @@ async def main():
ack_message
=
Ack
(
CURRENCY
,
signing_key
.
pubkey
,
remote_connect_document
.
challenge
).
get_signed_json
(
signing_key
)
#
s
end ACK message
#
S
end ACK message
print
(
"
Send ACK message...
"
)
await
ws
.
send_str
(
ack_message
)
...
...
@@ -134,114 +146,111 @@ async def main():
# Connection error
print
(
"
Web socket connection error !
"
)
# END HANDSHAKE #######################################################
print
(
"
END OF HANDSHAKE
\n
"
)
# send ws2p request
# Send ws2p request
print
(
"
Send getCurrent() request
"
)
request_id
=
get_ws2p_challenge
()[:
8
]
await
ws
.
send_str
(
requests
.
get_current
(
request_id
))
#
w
ait response with request id
#
W
ait response with request id
response_str
=
await
ws
.
receive_str
()
while
"
resId
"
not
in
json
.
loads
(
response_str
)
or
(
"
resId
"
in
json
.
loads
(
response_str
)
and
json
.
loads
(
response_str
)[
"
resId
"
]
!=
request_id
):
response_str
=
await
ws
.
receive_str
()
time
.
sleep
(
1
)
try
:
#
c
heck response format
#
C
heck response format
parse_text
(
response_str
,
requests
.
BLOCK_RESPONSE_SCHEMA
)
#
i
f valid display response
#
I
f valid display response
print
(
"
Response:
"
+
response_str
)
except
ValidationError
as
exception
:
#
i
f invalid response...
#
I
f invalid response...
try
:
#
c
heck error response format
#
C
heck error response format
parse_text
(
response_str
,
requests
.
ERROR_RESPONSE_SCHEMA
)
#
i
f valid, display error response
#
I
f valid, display error response
print
(
"
Error response:
"
+
response_str
)
except
ValidationError
as
e
:
#
i
f invalid, display exception on response validation
#
I
f invalid, display exception on response validation
print
(
exception
)
#
s
end ws2p request
#
S
end ws2p request
print
(
"
Send getBlock(360000) request
"
)
request_id
=
get_ws2p_challenge
()[:
8
]
await
ws
.
send_str
(
requests
.
get_block
(
request_id
,
360000
))
#
w
ait response with request id
#
W
ait response with request id
response_str
=
await
ws
.
receive_str
()
while
"
resId
"
not
in
json
.
loads
(
response_str
)
or
(
"
resId
"
in
json
.
loads
(
response_str
)
and
json
.
loads
(
response_str
)[
"
resId
"
]
!=
request_id
):
response_str
=
await
ws
.
receive_str
()
time
.
sleep
(
1
)
try
:
#
c
heck response format
#
C
heck response format
parse_text
(
response_str
,
requests
.
BLOCK_RESPONSE_SCHEMA
)
#
i
f valid display response
#
I
f valid display response
print
(
"
Response:
"
+
response_str
)
except
ValidationError
as
exception
:
#
i
f invalid response...
#
I
f invalid response...
try
:
#
c
heck error response format
#
C
heck error response format
parse_text
(
response_str
,
requests
.
ERROR_RESPONSE_SCHEMA
)
#
i
f valid, display error response
#
I
f valid, display error response
print
(
"
Error response:
"
+
response_str
)
except
ValidationError
as
e
:
#
i
f invalid, display exception on response validation
#
I
f invalid, display exception on response validation
print
(
exception
)
#
s
end ws2p request
#
S
end ws2p request
print
(
"
Send getBlocks(360000, 2) request
"
)
request_id
=
get_ws2p_challenge
()[:
8
]
await
ws
.
send_str
(
requests
.
get_blocks
(
request_id
,
360000
,
2
))
#
w
ait response with request id
#
W
ait response with request id
response_str
=
await
ws
.
receive_str
()
while
"
resId
"
not
in
json
.
loads
(
response_str
)
or
(
"
resId
"
in
json
.
loads
(
response_str
)
and
json
.
loads
(
response_str
)[
"
resId
"
]
!=
request_id
):
response_str
=
await
ws
.
receive_str
()
time
.
sleep
(
1
)
try
:
#
c
heck response format
#
C
heck response format
parse_text
(
response_str
,
requests
.
BLOCKS_RESPONSE_SCHEMA
)
#
i
f valid display response
#
I
f valid display response
print
(
"
Response:
"
+
response_str
)
except
ValidationError
as
exception
:
#
i
f invalid response...
#
I
f invalid response...
try
:
#
c
heck error response format
#
C
heck error response format
parse_text
(
response_str
,
requests
.
ERROR_RESPONSE_SCHEMA
)
#
i
f valid, display error response
#
I
f valid, display error response
print
(
"
Error response:
"
+
response_str
)
except
ValidationError
as
e
:
#
i
f invalid, display exception on response validation
#
I
f invalid, display exception on response validation
print
(
exception
)
#
s
end ws2p request
#
S
end ws2p request
print
(
"
Send getRequirementsPending(3) request
"
)
request_id
=
get_ws2p_challenge
()[:
8
]
await
ws
.
send_str
(
requests
.
get_requirements_pending
(
request_id
,
3
))
#
w
ait response with request id
#
W
ait response with request id
response_str
=
await
ws
.
receive_str
()
while
"
resId
"
not
in
json
.
loads
(
response_str
)
or
(
"
resId
"
in
json
.
loads
(
response_str
)
and
json
.
loads
(
response_str
)[
"
resId
"
]
!=
request_id
):
response_str
=
await
ws
.
receive_str
()
time
.
sleep
(
1
)
try
:
#
c
heck response format
#
C
heck response format
parse_text
(
response_str
,
requests
.
REQUIREMENTS_RESPONSE_SCHEMA
)
#
i
f valid display response
#
I
f valid display response
print
(
"
Response:
"
+
response_str
)
except
ValidationError
as
exception
:
#
i
f invalid response...
#
I
f invalid response...
try
:
#
c
heck error response format
#
C
heck error response format
parse_text
(
response_str
,
requests
.
ERROR_RESPONSE_SCHEMA
)
#
i
f valid, display error response
#
I
f valid, display error response
print
(
"
Error response:
"
+
response_str
)
except
ValidationError
as
e
:
#
i
f invalid, display exception on response validation
#
I
f invalid, display exception on response validation
print
(
exception
)
# Close session
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment