import subprocess
host = "yahoo.com"
ping = subprocess.Popen(
["ping", "-c", "3", host],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
out, error = ping.communicate()
print out
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有这个用于对服务器进行 ping 测试
基本上:
看看这个,
每次我想查询产品价格时,我都会向 f3 的 API 发送一个包含产品详细信息的 HTTP GET 请求。我正在尝试确定在接到我的电话后检索产品价格信息需要多长时间。
如果你只需要 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
Run Code Online (Sandbox Code Playgroud)
有关更多信息,您可以查看请求文档
如果您正在寻找一般性的某些动作的计时,您可以使用这个简单的装饰器
对于当前的python,使用 print(f'Call time: {result.elapsed}')
可选地,使用 result.elapsed.total_seconds()
请参阅如何测量 Python 请求的服务器响应时间 POST-request
我不确定我是否很好地理解你的问题,但如果我是对的,你想要的可以使用模块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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5813 次 |
| 最近记录: |