[tor-commits] [stem/master] Add temporary scripts for testing async communication with sockets

atagar at torproject.org atagar at torproject.org
Thu Jul 16 01:28:57 UTC 2020


commit 89bc644ea55f1443e6bdc0279a401b9fef5f154a
Author: Illia Volochii <illia.volochii at gmail.com>
Date:   Sat Mar 14 22:05:15 2020 +0200

    Add temporary scripts for testing async communication with sockets
---
 test_control_port.py        | 30 ++++++++++++++++++++++++++++++
 test_control_socket_file.py | 30 ++++++++++++++++++++++++++++++
 test_relay_socket.py        | 28 ++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+)

diff --git a/test_control_port.py b/test_control_port.py
new file mode 100644
index 00000000..85fe26d4
--- /dev/null
+++ b/test_control_port.py
@@ -0,0 +1,30 @@
+import asyncio
+
+from stem.async_socket import ControlPort
+
+
+async def run_command(i, command):
+  async with ControlPort() as cp:
+    print(f'{i} Connecting')
+    await cp.connect()
+    print(f'{i} Authenticating')
+    await cp.send('AUTHENTICATE "password"')
+    print(f'{i} Receiving auth message')
+    await cp.recv()
+    print(f'{i} Sending the command')
+    await cp.send(command)
+    print(f'{i} Receiving result of the command')
+    result = await cp.recv()
+    print(f'{i} {result.content()}')
+
+
+if __name__ == '__main__':
+  loop = asyncio.get_event_loop()
+  tasks = asyncio.gather(
+    run_command(1, 'PROTOCOLINFO 1'),
+    run_command(2, 'GETINFO traffic/read'),
+  )
+  try:
+    loop.run_until_complete(tasks)
+  finally:
+    loop.close()
diff --git a/test_control_socket_file.py b/test_control_socket_file.py
new file mode 100644
index 00000000..4c6816f4
--- /dev/null
+++ b/test_control_socket_file.py
@@ -0,0 +1,30 @@
+import asyncio
+
+from stem.async_socket import ControlSocketFile
+
+
+async def run_command(i, command):
+  async with ControlSocketFile() as cp:
+    print(f'{i} Connecting')
+    await cp.connect()
+    print(f'{i} Authenticating')
+    await cp.send('AUTHENTICATE "password"')
+    print(f'{i} Receiving auth message')
+    await cp.recv()
+    print(f'{i} Sending the command')
+    await cp.send(command)
+    print(f'{i} Receiving result of the command')
+    result = await cp.recv()
+    print(f'{i} {result.content()}')
+
+
+if __name__ == '__main__':
+  loop = asyncio.get_event_loop()
+  tasks = asyncio.gather(
+    run_command(1, 'PROTOCOLINFO 1'),
+    run_command(2, 'GETINFO traffic/read'),
+  )
+  try:
+    loop.run_until_complete(tasks)
+  finally:
+    loop.close()
diff --git a/test_relay_socket.py b/test_relay_socket.py
new file mode 100644
index 00000000..76289d46
--- /dev/null
+++ b/test_relay_socket.py
@@ -0,0 +1,28 @@
+import asyncio
+
+from stem.client import DEFAULT_LINK_PROTOCOLS
+from stem.client.cell import VersionsCell
+from stem.async_socket import RelaySocket
+
+
+async def run_command(i, command):
+  async with RelaySocket(address='127.0.0.1', port=443) as cp:
+    print(f'{i} Connecting')
+    await cp.connect()
+    print(f'{i} Sending the command')
+    await cp.send(command)
+    print(f'{i} Receiving result of the command')
+    result = await cp.recv(2)
+    print(result)
+
+
+if __name__ == '__main__':
+  loop = asyncio.get_event_loop()
+  tasks = asyncio.gather(
+    run_command(1, VersionsCell(DEFAULT_LINK_PROTOCOLS).pack(2)),
+    run_command(2, VersionsCell(DEFAULT_LINK_PROTOCOLS).pack(2)),
+  )
+  try:
+    loop.run_until_complete(tasks)
+  finally:
+    loop.close()





More information about the tor-commits mailing list