I want to build a python http server (using Django or Flask or etc.) which I'll call it X. Also there's another python service on another machine, which I'll call Y, and there's an HTTP server Z running on a machine accessible only by Y. I want X to imitate Z. More formally: when X receives a request on http://x/PATH, I want to serialize the whole request (method, headers, cookies, body, etc.) into a binary string, transmit it to Y over a secure connection, Y make the same exact request to http://z/PATH, serialize the whole response (again including headers, etc.) into a binary string and transmit it to X over a secure channel, and X servers the same response to the client, almost as if client is connecting Z rather than X.
This is practically a proxy, but I want to be able to do all of these using a custom communication channel between X and Y that I've developed (which uses websockets and thus is full-duplex). I mean, be able to use any communication channel as long as it supports transmitting strings. I'm open to use SOCKS and etc. I just don't know how. I need technical details rather than just ideas.
Also, I'm not currently insisting on also supporting websockets but it'd be neat if I could.