-
Notifications
You must be signed in to change notification settings - Fork 0
/
scratch_1.py
77 lines (57 loc) · 2.54 KB
/
scratch_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import asyncio
import json
import websockets
self_address_request = json.dumps({
"type": "selfAddress"
})
async def send_text_without_reply():
message = "Hello Nym!"
uri = "ws://localhost:1977"
async with websockets.connect(uri) as websocket:
await websocket.send(self_address_request)
self_address = json.loads(await websocket.recv())
print("our address is: {}".format(self_address["address"]))
text_send = json.dumps({
"type": "send",
"message": message,
"recipient": self_address["address"],
"withReplySurb": False,
})
print("sending '{}' (*without* reply SURB) over the mix network...".format(message))
await websocket.send(text_send)
print("waiting to receive a message from the mix network...")
received_message = await websocket.recv()
print("received '{}' from the mix network".format(received_message))
async def send_text_with_reply():
message = "Hello Nym!"
uri = "ws://localhost:1977"
async with websockets.connect(uri) as websocket:
await websocket.send(self_address_request)
self_address = json.loads(await websocket.recv())
print("our address is: {}".format(self_address["address"]))
text_send = json.dumps({
"type": "send",
"message": message,
"recipient": self_address["address"],
"withReplySurb": True,
})
print("sending '{}' (*with* reply SURB) over the mix network...".format(message))
await websocket.send(text_send)
print("waiting to receive a message from the mix network...")
received_message = json.loads(await websocket.recv())
print("received '{}' from the mix network".format(received_message))
# use the received surb to send an anonymous reply!
reply_surb = received_message["replySurb"]
reply_message = "hello from reply SURB!"
reply = json.dumps({
"type": "reply",
"message": reply_message,
"replySurb": reply_surb
})
print("sending '{}' (using reply SURB!) over the mix network...".format(reply_message))
await websocket.send(reply)
print("waiting to receive a message from the mix network...")
received_message = await websocket.recv()
print("received '{}' from the mix network".format(received_message))
# asyncio.get_event_loop().run_until_complete(send_text_without_reply())
asyncio.get_event_loop().run_until_complete(send_text_with_reply())