I`m trying to make some kind of specialized proxy server for my needs. Application listen to some port and when I connect there using (for example) cURL it opens a socket (lets call it "my_socket" ). I get some data from there, modify it according to my needs and make a request to another server using HTTPS:
my_request = "https://whatever.com/params"
my_response = requests.get(my_request)
Now I want to send my_response with all headers and payload to the open socket. Some kind of
my_socket.send(my_response)
but socket accepts byte-like objects and my_response is HTTPResponse. This cause error. Could anybody advise me how to convert HTTPResponce to bytes or may be I can use any other approach to transfer my data? I think I can manually extract all the data from my_response and manually assemble the response, but maybe there is any easier way? Thanks.
Update I tried
my_socket.send(my_response.content)
but other side can not decode it to any text. It looks like this:
curl -o /dev/null -v -s -k "http://127.0.0.1:8080/my_cool_request"
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /my_cool_request HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.47.0
> Accept: */*
>
{ [16380 bytes data]
* Connection #0 to host 127.0.0.1 left intact
Update2. I send headers from my_response.headers.
my_lenth=len(my_response.content)
my_header="HTTP/1.1 " + str(my_response.status_code)+" " + my_response.reason
for key in my_response.headers:
my_header+=key+": "+r.headers[key]+"\n"
my_header+="Content-Length: "+str(my_lenth)+"\n"
header_encoded=my_header.encode()
my_socket.send (header_encoded)
my_socket.send(r.content)
This is not the final version, because I still get an error from my cURL:
* Illegal or missing hexadecimal sequence in chunked-encoding