Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A Python script using Requests to time downloads
#!/usr/bin/env python3
# time_download.py
# A simple script to time downloads in order to determine
# variances in the speed of the network connection used
# by the sensor machine. Reports in CSV
# YOU NEED TO CHANGE THE TARGET_URI VARIABLE
# Please, please, please don't use my cdn for more than a few downloads.
# You will end up getting your IP blocked.
import requests
import datetime
from time import perf_counter, sleep
from sys import argv
TARGET_URI = "http://<invalid>cdn.silverwingedseraph.net/randombits.bin"
if len(argv) != 3:
print("Wrong number of arguments; "
"give only a target file and an interval in seconds.")
exit(1)
try:
duration = float(argv[2])
except ValueError:
print("Duration must be a valid float.")
if duration < 10:
print("[ERROR]: duration less than 10 seconds is highly discouraged.")
exit(1)
elif duration < 60:
print("[WARNING]: duration less than 1 minute may cause a load problem"
" on the remote server.")
sleep(3)
print("[INFO]: Beginning data collection at {}".format(datetime.datetime.now()))
while (1):
t0 = perf_counter()
r = requests.get(TARGET_URI)
t1 = perf_counter()
now = datetime.datetime.now()
data = "{},{}\n".format(now, t1 - t0)
with open(argv[1], "a") as f:
f.write(data)
print("Wrote: " + data, end="")
sleep(duration)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment