Spaces:
Running
Running
File size: 2,992 Bytes
3289c58 |
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 |
#!/usr/bin/env python3
"""
Test E2B sandbox connectivity to ngrok proxy.
This script runs inside an E2B sandbox and attempts to call the ngrok proxy endpoint.
"""
import json
import urllib.request
import urllib.error
import time
def test_ngrok_call():
"""Call the ngrok proxy endpoint from E2B sandbox."""
url = "https://lieselotte-colligative-shabbily.ngrok-free.dev/functions/call"
headers = {"accept": "application/json", "Content-Type": "application/json"}
payload = {
"function_name": "digital_sales_get_my_accounts_my_accounts_get",
"app_name": "digital_sales",
"args": {},
}
print(f"[TEST] Starting request to {url}")
print(f"[TEST] Payload: {json.dumps(payload, indent=2)}")
print(f"[TEST] Headers: {json.dumps(headers, indent=2)}")
start_time = time.time()
data = json.dumps(payload).encode('utf-8')
req = urllib.request.Request(url, data=data, headers=headers, method='POST')
try:
print(f"[TEST] Opening connection at {time.time() - start_time:.2f}s...")
with urllib.request.urlopen(req, timeout=30) as response:
elapsed = time.time() - start_time
print(f"[TEST] Got response in {elapsed:.2f}s, status: {response.status}")
response_data = response.read().decode('utf-8')
print(f"[TEST] Body read, total time: {time.time() - start_time:.2f}s")
try:
result = json.loads(response_data)
print("[TEST] Success! Response JSON:")
print(json.dumps(result, indent=2))
return result
except Exception as parse_error:
print(f"[TEST] JSON parse error: {parse_error}")
print(f"[TEST] Raw response: {response_data}")
return response_data
except urllib.error.HTTPError as e:
elapsed = time.time() - start_time
print(f"[TEST] HTTP Error after {elapsed:.2f}s: {e.code} {e.reason}")
error_body = ""
try:
error_body = e.read().decode('utf-8')
print(f"[TEST] Error body: {error_body}")
except Exception:
pass
raise
except urllib.error.URLError as e:
elapsed = time.time() - start_time
print(f"[TEST] URL Error after {elapsed:.2f}s: {e.reason}")
raise
except Exception as e:
elapsed = time.time() - start_time
print(f"[TEST] Unexpected error after {elapsed:.2f}s: {type(e).__name__}: {str(e)}")
raise
if __name__ == "__main__":
print("=" * 80)
print("E2B SANDBOX NGROK CONNECTIVITY TEST")
print("=" * 80)
try:
result = test_ngrok_call()
print("\n" + "=" * 80)
print("TEST PASSED - Connection successful!")
print("=" * 80)
except Exception as e:
print("\n" + "=" * 80)
print(f"TEST FAILED - {type(e).__name__}: {str(e)}")
print("=" * 80)
import traceback
traceback.print_exc()
|