0

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.

amirmd76
  • 11
  • 3

1 Answers1

1

You could use the requests library to create/send a duplicate of the message (showing get).

import requests 

# api-endpoint 
URL = "http://Z/PAth"
parameter_information= "some information"
PARAMS = {'address':parameter_information} 

# sending get request and saving the response as response object 
r = requests.get(url = URL, params = PARAMS) 
mattsap
  • 3,790
  • 1
  • 15
  • 36
  • 1
    My main issues is on how to get the request and response data and what parameters to consider, what to do with cookies and localstorage, etc. and that there's probably a better solution than to serialize all request data. – amirmd76 Jul 30 '19 at 18:07
  • This seems like to do what you are looking for: https://stackoverflow.com/questions/6656363/proxying-to-another-web-service-with-flask. The key is to loop through all headers and field data to send. – mattsap Jul 30 '19 at 18:30
  • How about cookies for instance? I'm not looking to send a single request, there may be more requests to the server and Z wants to use cookies and etc. – amirmd76 Jul 31 '19 at 05:04
  • You can set the cookies in the request. It's detailed in the other stackoverflow post. resp = requests.request( method=request.method, url=request.url.replace(request.host_url, 'new-domain.com'), headers={key: value for (key, value) in request.headers if key != 'Host'}, data=request.get_data(), cookies=request.cookies, allow_redirects=False) – mattsap Jul 31 '19 at 13:45