4
import subprocess

host = "yahoo.com"

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

out, error = ping.communicate()
print out

So far I have this for a ping test to a server

basically:

Take a look at this,

Everytime I look want to look up prices of products, i send a HTTP GET request with the product details to f3's API. I am trying to determine how long it takes to retrieve the product price information after receiving my call.

4

2 回答 2

5

如果只需要 HTTP ping,可以使用优秀的 Python 库,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 response

    return json_result

有关更多信息,您可以查看请求文档

如果您通常在寻找一些动作的时间,您可以使用这个简单的装饰器

对于当前的 python,使用print(f'Call time: {result.elapsed}')

可选地,使用result.elapsed.total_seconds()

请参阅如何测量 Python 请求的服务器响应时间 POST-request

于 2013-09-17T19:10:14.577 回答
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
于 2013-09-17T19:06:08.463 回答