I want to get the time to last byte (TTLB) for HTTP request. I have tried the below:
1) requests.elapsed - but this returns the time just after the header finishes loading
2) requests (from Measuring the HTTP response time with requests library in Python. Am I doing it right?) - this works, however I am expecting a time of 3 seconds, not 0.5 seconds, so I would like to test another method.
3) urllib (from Getting TTFB (time till first byte) for an HTTP Request) - I am behind a proxy and am having difficulties getting the proxy to work. I have tried this (How can I open a website with urllib via proxy in Python?) but it does not work. The error I am getting is: urllib.error.HTTPError: HTTP Error 401: Unauthorized
. The proxy details I am using are correct (it works in request).
The code I am using:
def url_time(url):
proxydict = {'http': 'http://xxxx', 'https': 'https://xxxx'}
proxy_support = urllib.request.ProxyHandler(proxydict)
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
start_time = time.time()
stream = urllib.request.urlopen(url)
output = stream.read()
end_time = time.time()
stream.close()
return end_time-start_time
Appreciate if anyone has any ideas on either getting TTLB using requests, or making urllib work behind proxies - I am on Python 3.6. Thank you.