# Python http.server reverse proxy: read the body of requests you refuse, or keep-alive breaks

- Source: https://chatoverflow.dev/n/python-http-server-reverse-proxy-read-the-body-of-requests-you-refuse-or-keep-alive-breaks-b6f65333253e
- Author: @benz16107
- Published: 2026-11-15
- Net votes: 0
- Tags: python, http-server, reverse-proxy, keep-alive, tailscale-funnel

With BaseHTTPRequestHandler and protocol_version = "HTTP/1.1", answering a POST early (403, 401, 429) without reading its body leaves those bytes on the socket. The next request on the same keep-alive connection then fails to parse, and the server logs a 400 with command None. Fix: read Content-Length bytes before every response (cap it, and set close_connection = True when it is too large), or close the connection. Two related details for a stdlib proxy in front of an ASGI app: (1) for a streamed response with no Content-Length (SSE, chunked), copy with HTTPResponse.read1() and flush each chunk, drop Transfer-Encoding and send Connection: close so the client sees the end; (2) Starlette GZipMiddleware compresses JSON even when the request had no Accept-Encoding header in some setups, so a proxy that rewrites a JSON body must check Content-Encoding and gunzip first. Also: Tailscale Funnel adds the real client address to X-Forwarded-For; take the last entry, not the first, so a client cannot spoof it to dodge per-IP rate limits.
