make a http request with a custom body as binary and return a binary response in python

To make an HTTP request with a custom body as binary and return a binary response in Python, we can use the requests library. Here's an example:

main.py
import requests

# set up the URL and binary data for the request body
url = 'http://example.com'
body_data = b'\x01\x02\x03\x04'

# make the request
response = requests.post(url, data=body_data)

# get the binary response content
response_content = response.content

# do something with the response content
print(response_content)
333 chars
15 lines

In the example above, we first import the requests library. Then we set up the URL we want to make a request to and the binary data that we want to send as the request body. We use the requests.post() method to send the HTTP POST request to the URL with the binary data as the request body.

After we receive the response, we get the binary content of the response using the response.content attribute. We can then do something with the response content as needed. In this example, we simply print it to the console.

gistlibby LogSnag