如何在python中确定GET请求的延迟

时间:2013-09-17 18:50:12

标签: python web-crawler latency

import subprocess

host = "yahoo.com"

ping = subprocess.Popen(
    ["ping", "-c", "3", host],
    stdout = subprocess.PIPE,
    stderr = subprocess.PIPE
)

out, error = ping.communicate()
print out

到目前为止,我已经将其用于对服务器进行ping测试

基本上:

看看这个,

每当我想要查询产品价格时,我都会将产品详细信息的HTTP GET请求发送到f3的API。我试图确定在接到我的电话后检索产品价格信息需要多长时间。

3 个答案:

答案 0 :(得分:2)

如果您只需要HTTP ping,则可以使用exelent Python libruary Requests

import requests, json

def call_some_api (params) :
    """ You can track time of any your API call. """

    result = requests.get("rest-api-endpoint.com")
        # Now result contains all request data you need


    print "Call time:", result.elapsed # .. prints timedelta ..
    json_result = json.load( result.text ) # Here are RESTful API, JSON responce

    return json_result

有关详细信息,请参阅requests docs

如果您正在寻找一般行动的时间安排,可以使用this simple decorators

答案 1 :(得分:1)

我不确定我是否理解你的问题,但如果我是对的,可以使用time模块中的time函数来实现你想要的,它会返回秒数自EPOCH以来已经过去了:

from time import time

time_before = time()
perform_get() # whatever way you do this
time_after = time()
time_taken = time_after-time_before

print time_taken

答案 2 :(得分:0)

import requests
r = requests.get('YOUR_URL')
print(r.elapsed)