| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """Deliberately slow HTTP endpoint that mimics the HIS bdhealth REST gateway.
- Accepts a POST, swallows the body, waits SLEEP_SEC, then answers 200 JSON.
- Used to measure the real client-side timeout of UppPay's RestClient.Post.
- """
- import json
- import sys
- import time
- from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
- SLEEP_SEC = int(sys.argv[1]) if len(sys.argv) > 1 else 60
- PORT = int(sys.argv[2]) if len(sys.argv) > 2 else 8099
- if hasattr(sys.stdout, "reconfigure"):
- sys.stdout.reconfigure(encoding="utf-8", errors="replace")
- class Handler(BaseHTTPRequestHandler):
- protocol_version = "HTTP/1.1"
- def do_POST(self):
- length = int(self.headers.get("Content-Length", 0))
- self.rfile.read(length)
- print("[slow-server] got POST %s (%d bytes) -> sleeping %ds"
- % (self.path, length, SLEEP_SEC), flush=True)
- time.sleep(SLEEP_SEC)
- body = json.dumps({"errorCode": "0", "errorMessage": "slow-ok"}).encode()
- try:
- self.send_response(200)
- self.send_header("Content-Type", "application/json")
- self.send_header("Content-Length", str(len(body)))
- self.end_headers()
- self.wfile.write(body)
- print("[slow-server] responded after %ds" % SLEEP_SEC, flush=True)
- except Exception as e:
- print("[slow-server] client already gone: %s" % e, flush=True)
- def log_message(self, *a):
- pass
- if __name__ == "__main__":
- print("[slow-server] listening on 127.0.0.1:%d, will stall %ds"
- % (PORT, SLEEP_SEC), flush=True)
- ThreadingHTTPServer(("127.0.0.1", PORT), Handler).serve_forever()
|