Search code examples
pythondnsresponse-time

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


I have a list of domains and I need to get some stat for example average response time for the index page of each site about them.

I wanna to get the Time To First Byte for each domain. I searched a little but I did not find any complete answer to my question. Here is my funtion for calculating the response time of a host:

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

When I run it for google.com for example, I got this error:

google.com not found


Solution

  • When you post a question you need to include a Minimal, Complete, and Verifiable example of your code. Your code is none of these things. If I do the simple importing of libraries and defining of variables that would be required to create a Minimal, Complete, and Verifiable example of your code then it runs fine:

    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