-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
77 lines (62 loc) · 2.17 KB
/
example.py
File metadata and controls
77 lines (62 loc) · 2.17 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
import requests
def demonstrate_successful_execution():
"""
Demonstrates successful code execution using the API.
Shows how to execute valid code with proper local variables.
"""
url = "http://localhost:1729"
print("=== Successful Code Execution Examples ===\n")
# Example 1: Basic arithmetic
print("Example 1: Basic arithmetic")
print("Code: out=x+y")
print("Local variables: x=10, y=2")
payload = {
"code": "out=x+y",
"local_dict": {
"x": 10,
"y": 2
}
}
try:
response = requests.post(f'{url}/execute_code/', json=payload, timeout=10)
result = response.json()
print(f"Result: {result}")
except requests.exceptions.ConnectionError:
print("❌ Connection refused - server not running")
except requests.exceptions.Timeout:
print("❌ Request timeout - server not responding")
except Exception as e:
print(f"❌ Error: {e}")
print("\n" + "-"*50 + "\n")
def demonstrate_error_handling():
"""
Demonstrates error handling in code execution using the API.
Shows what happens when code contains errors or undefined variables.
"""
url = "http://localhost:1729" # Use localhost for consistency
print("=== Error Handling Examples ===\n")
# Example 1: Undefined variable
print("Example 1: Undefined variable")
print("Code: out=b")
print("Local variables: x=10, y=2 (but 'b' is not defined)")
payload = {
"code": "out=b",
"local_dict": {
"x": 10,
"y": 2
}
}
try:
response = requests.post(f'{url}/execute_code/', json=payload, timeout=10)
result = response.json()
print(f"Result: {result}")
except requests.exceptions.ConnectionError:
print("❌ Connection refused - server not running")
except requests.exceptions.Timeout:
print("❌ Request timeout - server not responding")
except Exception as e:
print(f"❌ Error: {e}")
print("\n" + "-"*50 + "\n")
if __name__ == "__main__":
demonstrate_successful_execution()
demonstrate_error_handling()