1

I am trying to see how many bytes were sent out to the server when making an HTTP request.

import requests

resp = requests.get("http://www.example.com")

print(resp.request)
>>> <PreparedRequest [GET]>

How can I get the number of bytes contained in <PreparedRequest [GET]> including any attached files, payloads, etc.

AlanSTACK
  • 5,525
  • 3
  • 40
  • 99
  • 1
    @MendelG Please re-read the question. – AlanSTACK Oct 15 '20 at 04:46
  • Are you interested in determining the total raw byte count for the request, or is it enough to know the sizes of the useful payload portions of the request? – CryptoFool Oct 15 '20 at 05:09
  • @Steve Currently trying to analyze traffic of a particular library. Trying to get an **APPROXIMATE** value for number of bytes outbound/inbound. I am using `len(resp.content)` for inbound. Don't know how to do outbound correctly right now. – AlanSTACK Oct 15 '20 at 05:10
  • 1
    If you want to get pretty much the exact size of the request, including all the structure, I think this question/answers might do it for you: https://stackoverflow.com/questions/20658572/python-requests-print-entire-http-request-raw. – CryptoFool Oct 15 '20 at 05:17
  • If you want more of an approximation, then knowing the structure of what you're sending will let you get that. You mention a GET request as an example. A GET request has no body. PUT and POST requests have a body, but you should know what you're putting in there, so you should know the size. I guess there could be cases where the body of a PUT or POST is being constructed for you and so you don't know just how big they are, but I think you can get at the Content-Length header once the request is sent to get the size of the request body. – CryptoFool Oct 15 '20 at 05:20
  • @Steve Really, there is no in-built method to calculate such a trivial property? I have to compute it by hand using the RFC? This is absurd... – AlanSTACK Oct 15 '20 at 05:23
  • I was just looking at that URL I gave you above, and the Requests docs. I think you can get the exact size of the payload once you've sent the request via `len(resp.request.content)` or maybe `len(resp.request.text)`. For a GET request, these two fields will both be `None`, as a GET doesn't have a body. – CryptoFool Oct 15 '20 at 05:28
  • I agree with @Steve 's answer. Otherwise you can calculate the size of your payload before sending request by converting it into bytes or something else which you want. – Adam Strauss Oct 15 '20 at 05:53

2 Answers2

0

len(resp.content) should work !

0

I will answer my own question. Although it is not perfect and has several scenarios in which it fails to account for edge cases, it works for my needs.

def rlen(response):
    """
    approximate request size sent to server
    """
    len_of_meth = len(response.request.method)
    len_of_addr = len(response.request.url)
    len_of_head = len('\r\n'.join('{}{}'.format(k, v) for k, v in response.request.headers.items()))
    len_of_body = len(response.request.body if response.request.body else [])

    return len_of_meth + len_of_addr + len_of_head + len_of_body
AlanSTACK
  • 5,525
  • 3
  • 40
  • 99