Measure WiFi speed with Python

This article is the 13th day article of JSL \ (Nippon System Giken ) Advent Calendar 2019.


Recently, I felt that there was a timing when the WiFi in the company slowed down, but I did not know quantitatively how slow it was, so I wrote a script to run a speed test regularly.

First summary

-I wrote a program in Python to measure the speed of in-house WiFi using Speedtest CLI -Internet connection measurement for developers. --It is convenient to be able to execute a speed test from CUI

Speed test from CUI

First of all, I wanted to run it regularly with cron etc., so I thought about running a speed test with CUI

In my image, the speed test was measured as follows: search → execute….

スクリーンショット 2019-12-23 15.54.09.png

I tried to organize the means to measure the network line speed on Linux | Developers \ .IO ) It seems that speedtest.net provides the CLI when I investigated while looking at the article etc.

The Speedtest CLI also offers a Python package,

--It is possible to install binary with pip --It seems that you can use the speedtest command in a virtual environment without managing it with brew. --It seems that it can be imported on Python

I found out

Build speedtest execution environment

I built an environment using pipenv

$ mkdir wifi_speedtest
$ cd $_  
#If pipenv is not installed
# brew install pipenv

#Create virtual environment
$ pipenv --python 3.7

#Package installation
$ pipenv install speedtest-cli

#Enable virtual environment
$ pipenv shell

#test the speedtest command
(.venv) $ speedtest --version
speedtest-cli 2.1.2
Python 3.7.4 (default, Oct 12 2019, 18:55:28) [Clang 11.0.0 (clang-1100.0.33.8)]

Try running Speedtest on the CUI

You can get a list of connection destination servers provided by volunteers with speedtest --list, so decide on one and make a note of the number.

(.venv) $ speedtest --list | grep Tokyo
15047) OPEN Project (via 20G SINET) (Tokyo, Japan) [6.34 km]
24333) Rakuten Mobile , Inc (Tokyo, Japan) [6.34 km]
28910) fdcservers.net (Tokyo, Japan) [6.34 km]
18516) GIAM PING VIETPN.COM (Tokyo, Japan) [6.34 km]
22247) Tokyonet (Castro, Brazil) [18486.74 km]

If you specify as follows, the speed test will always be executed on the specified server. For example, for a server provided by Rakuten Mobile, Inc, the number would be 24333.

#Run speed test
(.venv) $ speedtest --server 24333
Retrieving speedtest.net configuration...
Testing from XXX (xx.xx.xx.xx)...
Retrieving speedtest.net server list...
Retrieving information for the selected server...
Hosted by Rakuten Mobile , Inc (Tokyo) [6.34 km]: 200.123 ms
Testing download speed................................................................................
Download: 16.08 Mbit/s
Testing upload speed......................................................................................................
Upload: 31.96 Mbit/s

I was able to measure safely (some client information is hidden)

However, this is difficult to handle in the program, so I will add --json and get the result in json format

(.venv) $ speedtest --server 24333 --json

result


{"download": 20208058.464686207, "upload": 54426180.687909536, "ping": 48.215, "server": {"url": "http://ookla.mbspeed.net:8080/speedtest/upload.php", "lat": "35.6833", "lon": "139.6833", "name": "Tokyo", "country": "Japan", "cc": "JP", "sponsor": "Rakuten Mobile , Inc", "id": "24333", "host": "ookla.mbspeed.net:8080", "d": 6.336536019993832, "latency": 48.215}, "timestamp": "2019-12-23T07:23:16.316637Z", "bytes_sent": 68534272, "bytes_received": 25353712, "share": null, "client": {"ip": "xx.xx.xx.xx", "lat": "xx.xxxx", "lon": "xxx.xxxx", "isp": "XXX", "isprating": "3.7", "rating": "0", "ispdlavg": "0", "ispulavg": "0", "loggedin": "0", "country": "JP"}}

Consider dealing with this on Python

Run Speedtest in Python

Since it is provided as a Python Package, I thought about executing a speed test in Python in a pluggable manner, but this time I thought about handling the execution result in CUI as it is, so speedtest with subprocess module Decided to run the command

subprocess ---subprocess management — Python 3 \ .8 \ .1 documentation

wifi_speedtest.py


def get_speedtest_result():
    process = subprocess.run(['speedtest', '--server', '24333', '--json'], capture_output=True)
    return json.loads(process.stdout)


def bit_to_mbit(bit):
    """
Making a feeling that does not care about errors
    """
    return bit / 1024 / 1024


result = get_speedtest_result()
print(bit_to_mbit(result["download"]), bit_to_mbit(result["upload"]))

result


13.46394215 50.48002296

There are several ways to execute a command with subprocess, but this time I got the standard output withrun ()+ capture_output = True, which is officially mentioned in the usage example.

This time, I hit the API of SpreadSheet to record it (details are omitted below).

-Edit Google Spreadsheets in Python -Qiita

I think it's a good idea to skip to Slack and record it in any way you like.


As an aside, using Pipenv's scripts was convenient because it was not necessary to enable the virtual environment when executing from cron (processes executed via pipenv run are treated the same as execution in virtual environment).

Pipfile


[scripts]
dev = 'python wifi_speedtest.py'

Recommended Posts

Measure WiFi speed with Python
python3 Measure the processing speed.
Roughly speed up Python with numba
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
How to measure execution time with Python Part 1
Explosive speed with Python (Bottle)! Web API development
How to measure execution time with Python Part 2
Measure CPU temperature of Raspberry Pi with Python
Serial communication with Python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
Python ~ Grammar speed learning ~
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Getting Started with Python
Use DynamoDB with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python
Primality test with python