如何使用 python 获取域列表的第一个字节的时间

How to get Time To First Byte of a list of domains with python

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

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

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 未找到

当您 post 一个问题时,您需要包含一个 Minimal, Complete, and Verifiable 您的代码示例。您的代码是这些东西的 none。如果我简单地导入库并定义创建代码的 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)

Returns:

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