Reputation:
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.
Upvotes: 5
Views: 10164
Reputation: 3245
If you need only HTTP ping, you can use the excellent Python library, 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
For more information you can see requests docs
If you looking for timing some action in general, you can use this simple decorators
For current python, use print(f'Call time: {result.elapsed}')
Optionally, use result.elapsed.total_seconds()
See How to measure server response time for Python requests POST-request
Upvotes: 5
Reputation: 18029
I'm not sure I understand your question well, but if I'm right, what you want can be achieved using the time
function from the time
module, which returns the number of seconds elapsed since 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
Upvotes: 1