The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 07:15 UTC
| 1 | #!/usr/bin/env python3 |
| 2 | """SageMaker serving contract: GET /ping and POST /invocations on port 8080. |
| 3 | |
| 4 | /invocations echoes a JSON request body back as {"echo": <body>}. |
| 5 | """ |
| 6 | import json |
| 7 | import os |
| 8 | from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer |
| 9 | |
| 10 | |
| 11 | class Handler(BaseHTTPRequestHandler): |
| 12 | protocol_version = "HTTP/1.1" |
| 13 | |
| 14 | def _send(self, status, body, content_type="application/json"): |
| 15 | payload = body if isinstance(body, bytes) else json.dumps(body).encode() |
| 16 | self.send_response(status) |
| 17 | self.send_header("Content-Type", content_type) |
| 18 | self.send_header("Content-Length", str(len(payload))) |
| 19 | self.end_headers() |
| 20 | self.wfile.write(payload) |
| 21 | |
| 22 | def do_GET(self): |
| 23 | if self.path == "/ping": |
| 24 | self._send(200, {"status": "healthy"}) |
| 25 | else: |
| 26 | self._send(404, {"error": "not found"}) |
| 27 | |
| 28 | def do_POST(self): |
| 29 | if self.path != "/invocations": |
| 30 | self._send(404, {"error": "not found"}) |
| 31 | return |
| 32 | length = int(self.headers.get("Content-Length") or 0) |
| 33 | raw = self.rfile.read(length) if length > 0 else b"" |
| 34 | try: |
| 35 | request = json.loads(raw) if raw else None |
| 36 | except ValueError: |
| 37 | self._send(400, {"error": "request body must be JSON"}) |
| 38 | return |
| 39 | self._send(200, {"echo": request}) |
| 40 | |
| 41 | def log_message(self, format, *args): |
| 42 | pass |
| 43 | |
| 44 | |
| 45 | if __name__ == "__main__": |
| 46 | port = int(os.environ.get("SAGEMAKER_BIND_TO_PORT", "8080")) |
| 47 | ThreadingHTTPServer(("0.0.0.0", port), Handler).serve_forever() |