2
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.

teac
  • 45
  • 5
  • 1
    I think you're trying to write your own ping tool. There are operating system utilities that do just that: measure the time it takes for a request to reach a server. It would be cleaner to call such an os tool, for example with the python subprocess module. I found this existing question and hope it helps you :) – lhk Jun 11 '20 at 12:27
  • 1
    You are a new contributor, welcome to stackoverflow. You may have seen that I have voted to close your question. This is by no means meant to be rude. It is automatically done when suggesting that a question is a duplicate. I do think that your question is well posed: Nicely formatted with a good code example that shows your effort to solve it. So have my upvote :) – lhk Jun 11 '20 at 12:30

1 Answers1

0

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.

Jafoor
  • 695
  • 5
  • 14
  • yeah i understand that, I just added a snippet of entire code/file. My code works fine I was asking for a better method. – teac Jun 11 '20 at 12:37