Demo entry 6713841

python

   

Submitted by anonymous on Feb 10, 2018 at 09:58
Language: Python 3. Code size: 1.2 kB.

import pyspeedtest
import time
from datetime import datetime


def speed_test(delay):
    st = pyspeedtest.SpeedTest()
    current_time = str(datetime.now())[:-7]

    try:
        ping = st.ping()
        download = st.download()/1000000
        upload = st.upload()/1000000
        res = \
    """---------------------------------------------------
    time = {time} 
    ping = {ping:.3f} ms
    download = {download:.3f} Mbps
    upload = {upload:.3f} Mbps""".format(time=current_time, ping=ping, download=download, upload=upload)
    except Exception as e:
        res =\
    """---------------------------------------------------
    time = {time}
    Error: {error_message}""".format(time=current_time, error_message=str(e))
        time.sleep(delay/4)        # if fail wait and try again
        speed_test(delay)       

    print(res)  # print for console
    return res


def run(delay):
    i = 0
    while True:
        print(i)
        file = open('speed_test_log.txt', 'a')
        data = speed_test(delay) + '\n'
        file.write(data)
        file.close()
        time.sleep(delay)
        i += 1

run(60)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).