Requests time - python

Viewed 61
timings = []

for _ in range(10):
    name = requests.put("https://example.com", headers=auth)
    if name.status_code == 204:
        print(name.elapsed.total_seconds()*1000)
        timings.append(name.elapsed.total_seconds()*1000)
        time.sleep(30)
    else:
        print (name)
        print (name.text)
        time.sleep(30)


aver = sum(timings) / len(timings)
print()
print(f"{aver} total average")

Long story short: I need to hit a server with a request at a specific time. In order to do this I'm taking an average of the time it takes to send the request before hand, so I can send it that many seconds before so the server recieves it at the specific time.

Just wondering if there is a more precise/better way to do it as "elapsed.total_seconds()" also includes the time it takes for me to download the response where as I'm literally just looking for the time it takes for the server to recieve my request nothing else.

1 Answers

You must import requests. You can install requests first in your system.

pip install requests

Then import it in your code.

import requests

It should work then.

Related