How to get entire size of request message in python?
Question:
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.
Answers:
len(resp.content) should work !
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('rn'.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
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.
len(resp.content) should work !
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('rn'.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