Python请求:找出完整大小的请求

时间:2018-06-18 11:46:09

标签: python request

如何通过Python请求发送请求的完整大小(我没有要求回复)
我应该只添加body(如果适用)和headers的长度吗? 对于请求元素,还有什么需要考虑的事情吗?

这适用于我希望计算其大小的任何请求已发送(GET,POST,DELETE)。

1 个答案:

答案 0 :(得分:0)

Short answer - size of request is sum of mainly size of method, URL, headers and body (if apply) so in approximation it will be:

import requests

response = requests.get('https://httpbin.org/get')

method_len = len(response.request.method)
url_len = len(response.request.url)
headers_len = len('\r\n'.join('{}{}'.format(k, v) for k, v in response.request.headers.items()))
body_len = len(response.request.body if response.request.body else [])

print(f'Request size {method_len + url_len + headers_len + body_len}')

If anything more apply please post comment.

Resources:

RFC documentation https://tools.ietf.org/html/rfc7230#section-3

中反转用户名

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 库存储库成员响应: https://github.com/requests/requests/issues/4694

@Stack对这个问题的评论,他提到Python requests - print entire http request (raw)?

有关URI的问题:Is URL a part of request size?