Home>

I would like to create a program that measures the communication speed to the Internet with python and saves it in a CSV file.
After investigating various things, I found out that there is a Speedtest-cli, for the time being,
When I copied and executed the code, I was able to execute it and measure the speed.

However, the content of the code, the function of this Speedtest-cli, or the method
It is in a state where it is completely impossible to grasp what kind of operation is performed and what kind of value is returned.

For example, what does speedtest.Speedtest () do and what value does it return?

How can I find the explanation (documentation) about speedtest and speedtest?

Even if I checked the following site, the module (library) called speedtest was not posted. .. ..
https://docs.python.org/ja/3/py-modindex.html#cap-s

It is said that python has many useful modules (libraries),
It is not good to use it without knowing the contents, so each one
I would like to create it while understanding it.

Code that was copied and executed

import sys
import speedtest

def get_speed_test ():
    servers = []
    stest = speedtest.Speedtest ()
    stest.get_servers (servers)
    stest.get_best_server ()
    return stest
def command_line_runner ():
    stest = get_speed_test ()
    down_result = stest.download ()
    up_result = stest.upload ()
    mbps_down_result = down_result/1024/1024
    mbps_up_result = up_result/1024/1024
    result = [mbps_down_result, mbps_up_result]
    print (result)
command_line_runner ()

Execution result

57.158226870187505, 87.41375687289494
  • Answer # 1

    All the details of the operation are written in the source code.
    It seems that I sent a request to the api endpoint of the speed measurement site http://www.speedtest.net/ and processed the obtained result.

    How to use in python
    https://github.com/sivel/speedtest-cli/wiki
    It seems that it is written in

    I wrote a brief explanation while looking at the contents of the following source.
    (The command above is the explanation, and the comment below is the explanation)

    import speedtest
    s = speedtest.Speedtest ()
    #Create an object for speed measurement.
    servers = []
    # A list of server IDs. Specify in the argument of get_servers () function,
    # Specify the ID of the server used for measurement in the list.
    # You can refer to the specified server ID at https://www.speedtest.net/speedtest-servers-static.php.
    #Example: servers = [37950, 18709]
    threads = None
    # You can measure in parallel using the specified number of threads.
    # When measuring with a single thread, 1.
    If # None is specified, the value automatically set by speedtest.net will be set as the number of execution threads.
    s.get_servers (servers)
    Specifies the server ID described in the list specified in the # argument as the measurement server.
    s.get_best_server ()
    Use #ping to specify the server with the lowest latency as the measurement server.
    s.download (threads = threads)
    #Measure download speed and return speed in bits/sec. The number of threads specified in threads will be used.
    s.upload (threads = threads)
    # Measures upload speed and returns speed in bits/sec. The number of threads specified in threads will be used.
    s.results.share ()
    #Measurement result Generates a shared URL for the image.