【问题标题】:How to get Time To First Byte of a list of domains with python如何使用python获取域列表的第一个字节的时间
【发布时间】:2017-03-12 16:07:10
【问题描述】:

我有一个域列表,我需要获取一些统计数据,例如每个站点的索引页面的平均响应时间。

我想获取每个域的首字节时间。我搜索了一下,但我没有找到我的问题的任何完整答案。这是我计算主机响应时间的函数:

opener = urllib2.build_opener()


request = urllib2.Request("http://"+host)


start = time.time()


resp = opener.open(request)


# read one byte


resp.read(1)


ttfb = time.time() - start


# read the rest


resp.read()


ttlb = time.time() - start


print "The TTFirst Byte of " +host+"is:"+ttlb

例如,当我为 google.com 运行它时,我收到了这个错误:

找不到 google.com

【问题讨论】:

  • 您在寻求什么帮助?您是否尝试过实施任何东西?您可以 for 循环遍历要探测的域,然后将 print 输出到命令行?
  • 嗨@AriCooper-Davis,我编辑了我的问题。你知道我的错误吗?

标签: python dns response-time


【解决方案1】:

当您发布问题时,您需要包含 Minimal, Complete, and Verifiable 代码示例。你的代码不是这些东西。如果我简单地导入库并定义创建代码的最小、完整和可验证示例所需的变量,那么它运行良好:

import time
import urllib2

host = "http://google.com"
opener = urllib2.build_opener()
request = urllib2.Request(host)
start = time.time()
resp = opener.open(request)
# read one byte
resp.read(1)
ttfb = time.time() - start
# read the rest
resp.read()
ttlb = time.time() - start

print "The TTFirst Byte of " +host+" is: "+str(ttlb)

返回:

The TTFirst Byte of http://google.com is: 1.25

【讨论】:

  • 这对我有用。你能给我一个网址,我可以在你的答案上加个+分吗?
  • 我不明白你的问题,你在找什么网址?您可以通过按我答案左侧的勾号将我的答案标记为正确。
  • 好的,我很抱歉我的英语不好!看来我没有足够的声誉来标记答案。否则,我会在可能的情况下标记您的答案。
  • 别担心!你绝对可以接受答案。查看此图以了解如何接受答案 :-) meta.stackexchange.com/a/5235
猜你喜欢
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 2023-03-10
  • 1970-01-01
  • 2020-07-31
  • 1970-01-01
相关资源
最近更新 更多