I'm trying to gather some metrics regarding a network issue in a small LAN with SSH disconnecting occasionally and ping exhibiting huge delays (up to a minute instead of less than a second!).
Using timeit
(I've read that it's a good way to check elapsed execution time when calling some code snippet) I try to download some data from a FTP server that also runs locally, measure the time and store it inside a log file.
from ftplib import FTP
from timeit import timeit
from datetime import datetime
ftp = FTP(host='10.0.0.8')
ftp.login(user='****', passw='****')
ftp.cwd('updates/')
ftp.retrlines('LIST')
# Get timestamp when the download starts
curr_t = datetime.now()
print('Download large file and measure the time')
file_big_t = timeit(lambda f=ftp: f.retrbinary('RETR update_big', open('/tmp/update_big', 'wb').write))
print('Download medium file and measure the time')
file_medium_t = timeit(lambda f=ftp: f.retrbinary('RETR file_medium ', open('/tmp/file_medium ', 'wb').write))
print('Download small file and measure the time')
file_small_t = timeit(lambda f=ftp: f.retrbinary('RETR update_small', open('/tmp/update_small', 'wb').write))
# Write timestamp and measured timings to file
# ...
If I call the retrbinary(...)
without the timeit
it works without any issues. However the code above results in the script freezing right after the first timeit
call.