-1

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

There is an explanation why it is happening

  • This might be helpful [https://stackoverflow.com/questions/21233340/sending-string-via-socket-python](https://stackoverflow.com/questions/21233340/sending-string-via-socket-python)! –  Oct 18 '19 at 12:02

2 Answers2

0

the content variable of the Response-Class is a byte-like object:

response = requests.get("https://google.com")
mySocket.send(response.content)
Sofien
  • 478
  • 3
  • 12
  • Thanks for answer. I tried response.content, but it seems .content contains just a payload without headers. Therefore when I use this construction I do not get response code and other information. Just a payload. – ZeleniyZmey Oct 18 '19 at 14:02
0

As Sofian said, you can use response.content.
But in general, you should know that if you have a string object, you can do
str.encode() to make it a byte-like object;

x = "hello, world!"
x.encode()

Then to get it back to a string, use str.decode()

x = b"hello, world!"
print(x.decode())
ori6151
  • 592
  • 5
  • 11