首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何计算python脚本使用了多少网络IO?

如何计算python脚本使用了多少网络IO?
EN

Stack Overflow用户
提问于 2020-10-10 07:49:10
回答 1查看 167关注 0票数 0

假设我们有以下文件script.py

代码语言:javascript
复制
import requests

response = requests.get("https://example.com")

我可以通过测量response的大小来估计我已经使用了多少网络带宽。

但是如果脚本更复杂呢?这个脚本现在是一个极其复杂的二手文件,它使用了一堆不同的库。该脚本在处理信息并通过同一目录中的json文件返回结果之前,从各种不同的网站获取信息。

现在,我如何找出脚本每次运行使用了多少网络带宽?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-29 05:35:50

如果你想把事情做好,你必须自己去做。

代码语言:javascript
复制
# the following module imports are in-built

# the following module imports are in-house

# the following module imports are 3rd-party
import requests

total_sent_length = 0
total_recv_length = 0

get_sent_length   = 0
get_recv_length   = 0

post_sent_length  = 0
post_recv_length  = 0

old_request_method_get  = requests.get
old_request_method_post = requests.post

def format_size(bytes, suffix='B'):
    """
    return bytes in a human readable format
    """
    for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
        if abs(bytes) < 1024.0:
            return '%3.1f%s%s' % (bytes, unit, suffix)
        bytes /= 1024.0

    return '%.1f%s%s' % (bytes, 'Y', suffix)

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

def patched_get(*args, **kwargs):
    """
    delegate functionality and record stats
    """
    global total_sent_length
    global total_recv_length

    global get_sent_length
    global get_recv_length

    response = old_request_method_get(*args, **kwargs)

    total_sent_length += rlen(response)
    total_recv_length += len(response.content)

    get_sent_length += rlen(response)
    get_recv_length += len(response.content)

    return response

def patched_post(*args, **kwargs):
    """
    delegate functionality and record stats
    """
    global total_sent_length
    global total_recv_length

    global post_sent_length
    global post_recv_length

    response = old_request_method_post(*args, **kwargs)

    total_sent_length += rlen(response)
    total_recv_length += len(response.content)

    post_sent_length += rlen(response)
    post_recv_length += len(response.content)

    return response

requests.get  = patched_get
requests.post = patched_post

print('total_sent_length', format_size(total_sent_length))
print('total_recv_length', format_size(total_recv_length))

resp1 = requests.get('http://www.example.com/')
resp2 = requests.get('http://www.example.com/')

print('total_sent_length', format_size(total_sent_length))
print('total_recv_length', format_size(total_recv_length))

下面是示例结果

代码语言:javascript
复制
total_sent_length 0.0B
total_recv_length 0.0B
total_sent_length 242.0B
total_recv_length 2.5KB
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64288615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档