-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogrammatic_session.py
More file actions
executable file
·97 lines (80 loc) · 2.78 KB
/
Copy pathprogrammatic_session.py
File metadata and controls
executable file
·97 lines (80 loc) · 2.78 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
"""
Example: Programmatic Session Control
Demonstrates how to use non-interactive sessions for:
- Multi-step command sequences
- Inspecting session output programmatically
- Handling session state and errors
- Building automation scripts
"""
import sys
import time
from sandd import Server
def main():
# Start server
server = Server("0.0.0.0", 8765)
print(f"Server listening on {server.address}")
# Wait for daemon
daemon_id = "daemon-1"
print(f"\nWaiting for daemon '{daemon_id}'...")
if not server.wait_for_daemon(daemon_id, timeout=30):
print(f"Daemon '{daemon_id}' did not connect")
sys.exit(1)
print(f"Daemon '{daemon_id}' connected!\n")
# Create a non-interactive session
print("=== Creating Session ===")
session = server.new_session(daemon_id, rows=24, cols=80)
print("Session created\n")
# Example 1: Execute command and capture output
print("=== Example 1: Basic Command ===")
session.write(b"echo 'Hello from session'\n")
time.sleep(0.2)
output = session.read(timeout=1.0)
if output:
print(f"Output: {output.decode()}")
# Example 2: Multi-step workflow
print("\n=== Example 2: Multi-Step Workflow ===")
steps = [
("mkdir -p /tmp/test", "Creating directory"),
("cd /tmp/test", "Changing directory"),
("pwd", "Verifying location"),
("echo 'test' > file.txt", "Creating file"),
("cat file.txt", "Reading file"),
]
for cmd, description in steps:
print(f"{description}: {cmd}")
session.write(f"{cmd}\n".encode())
time.sleep(0.1)
output = session.read(timeout=1.0)
if output:
result = output.decode().strip()
if result:
print(f" → {result}")
# Example 3: Error handling
print("\n=== Example 3: Error Handling ===")
session.write(b"exit 42\n") # Exit with non-zero code
time.sleep(0.2)
# Try to write after exit - should fail gracefully
try:
session.write(b"echo 'after exit'\n")
output = session.read(timeout=1.0)
if output:
print(f"Output: {output.decode()}")
except Exception as e:
print(f"Session closed (expected): {e}")
# Example 4: Create new session for long-running task
print("\n=== Example 4: Long-Running Task ===")
session2 = server.new_session(daemon_id)
session2.write(b"for i in 1 2 3; do echo \"Step $i\"; sleep 1; done\n")
# Stream output as it arrives
start = time.time()
while time.time() - start < 5:
output = session2.read(timeout=0.5)
if output:
print(output.decode(), end='', flush=True)
else:
break
session2.close()
print("\n\nSession closed")
if __name__ == "__main__":
main()