使用getheader()方法从HTTP响应中提取响应时间的示例代码
import urllib.request
import time
url = "https://www.example.com"
start_time = time.time()
response = urllib.request.urlopen(url)
end_time = time.time()
response_time = end_time - start_time
print("Response Time: ", response_time)
# Example 1
# Fetch the response time of a website
url = "https://www.google.com"
start_time = time.time()
response = urllib.request.urlopen(url)
end_time = time.time()
response_time = end_time - start_time
print("Response Time (Google): ", response_time)
# Example 2
# Fetch the response time of an API
url = "https://api.example.com/data"
start_time = time.time()
response = urllib.request.urlopen(url)
end_time = time.time()
response_time = end_time - start_time
print("Response Time (API): ", response_time)
# Example 3
# Fetch the response time of a file download
url = "https://www.example.com/file.pdf"
start_time = time.time()
response = urllib.request.urlopen(url)
end_time = time.time()
response_time = end_time - start_time
print("Response Time (File Download): ", response_time)
