命令列下測ISP的網路速度 (SpeedTest For console)

命令列下測ISP的網路速度 (SpeedTest For console)

說到要測ISP網路速度會想到的就是speedtest這個專門在測試的網站,選擇適合的伺服器就能測試它的上傳與下載的速度實在很不錯。不過,現在已經有人利用Python寫出命令列下的版本,程式名為speedtest-cli

安裝

官網提供了幾種安裝方式

pip

pip install speedtest-cli

easy_install speedtest-cli

pip install git+https://github.com/sivel/speedtest-cli.git

wget

wget -O speedtest-cli https://raw.github.com/sivel/speedtest-cli/master/speedtest_cli.py
chmod +x speedtest-cli

curl

curl -o speedtest-cli https://raw.github.com/sivel/speedtest-cli/master/speedtest_cli.py
chmod +x speedtest-cli

在這我選擇wget的方式,下載後記得將屬性改成可執行的指令。

開始測試速度

直接執行speedtest-cli

danny:~$ ./speedtest-cli
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from CHTD, Chunghwa Telecom Co., Ltd. (xxx.xxx.218.xxx)...
Selecting best server based on ping...
Hosted by Taiwan Fixed Network (Taoyuan) [36.72 km]: 10.014 ms
Testing download speed........................................
Download: 9.53 Mbit/s
Testing upload speed..................................................
Upload: 3.38 Mbit/s
danny:~$

上面能看出來整個測試的訊息與結果:

上傳

Download: 9.53 Mbit/s

下載

Upload: 3.38 Mbit/s

進階設定

安裝完成後執行speedtest-cli -h得到更細節的操作方式

$ speedtest-cli -h
usage: speedtest-cli [-h] [--share] [--simple] [--list] [--server SERVER]
                     [--mini MINI] [--source SOURCE] [--version]

Command line interface for testing internet bandwidth using speedtest.net.
--------------------------------------------------------------------------
https://github.com/sivel/speedtest-cli

optional arguments:
  -h, --help       show this help message and exit
  --share          Generate and provide a URL to the speedtest.net share
                   results image
  --simple         Suppress verbose output, only show basic information
  --list           Display a list of speedtest.net servers sorted by distance
  --server SERVER  Specify a server ID to test against
  --mini MINI      URL of the Speedtest Mini server
  --source SOURCE  Source IP address to bind to
  --version        Show the version number and exit

這裡我們可以指定server ID來選擇測試的伺服器,先執行伺服器列表指定將所有的伺服器列出:

speedtest-cli --list

之後就可以看到一堆伺服器的列表

 941) Aeronet Broadband Corp (San Juan, Puerto Rico) [15144.11 km]
3807) HOTSPOTPR BROADBAND INTERNET (Bayamon, Puerto Rico) [15150.23 km]
3012) CLARO-PRT (Bayamon, Puerto Rico) [15150.23 km]
4122) Delvivi.net (Utuado, Puerto Rico) [15153.25 km]
2088) LibertyPR (Luquillo, Puerto Rico) [15158.67 km]
3712) LIME (Road Town, Virgin Islands) [15168.42 km]
2888) Innovative (Saint Thomas, Virgin Islands) [15175.24 km]
2884) Broadband VI (Saint Thomas, Virgin Islands) [15175.24 km]
2135) CLARO-PRT (Guaynabo, Puerto Rico) [15179.41 km]
3657) Open Mobile (Guaynabo, Puerto Rico) [15179.41 km]
:
:
:
未完
:

因為太多伺服器位址,在這我已經將台灣伺服器整理出來如下:

2182) kbro Co., Ltd. (Hsinchu, Taiwan)
2590) Far EasTone Telecommunications Co., Ltd (Hsinchu, Taiwan)
2934) Far Eastone Telecommunications Co., Ltd. (Neihu, Taiwan)
2589) Far EasTone Telecommunications Co., Ltd (Taoyuan, Taiwan)
3921) Taiwan Fixed Network (Taoyuan, Taiwan)
4505) Chief Telecom (New Taipei, Taiwan)
2327) Far Eastone Telecommunications Co., Ltd. (Taipei, Taiwan)
2188) TFN Media Co., Ltd. (Taipei, Taiwan)
2181) kbro Co., Ltd. (Taipei, Taiwan)
2133) Taiwan Fixed Network (Taipei, Taiwan)
3967) Chief Telecom (Taipei, Taiwan)
2187) TFN Media Co., Ltd. (Yilan, Taiwan)
3841) Taiwan Fixed Network (Taichung, Taiwan)
2192) kbro Co., Ltd. (Taichung, Taiwan)
2591) Far EasTone Telecommunications Co., Ltd (Taichung, Taiwan)
2595) Far EasTone Telecommunications Co., Ltd (Hualien, Taiwan)
2191) kbro Co., Ltd. (Hsinying, Taiwan)
2592) Far EasTone Telecommunications Co., Ltd (Tainan, Taiwan)
4413) Chief Telecom (Tainan, Taiwan)
2594) Far EasTone Telecommunications Co., Ltd (Pingtung, Taiwan)
2190) kbro Co., Ltd. (Pingtung, Taiwan)
2593) Far EasTone Telecommunications Co., Ltd (Kaohsiung, Taiwan)
3842) Taiwan Fixed Network (Kaohsiung, Taiwan)
2189) TFN Media Co., Ltd. (Kaohsiung, Taiwan)

最後我們再下指定伺服器的命令來針對某個伺服器測試ISP對它的速度:

speedtest-cli --server 2182

2181 為新竹的伺服器

2182) kbro Co., Ltd. (Hsinchu, Taiwan)

danny:~$ ./speedtest-cli --server 2182
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from CHTD, Chunghwa Telecom Co., Ltd. (xxx.xxx.218.xxx)...
Hosted by kbro Co., Ltd. (Hsinchu) : 10.888 ms
Testing download speed........................................
Download: 6.17 Mbit/s
Testing upload speed..................................................
Upload: 3.78 Mbit/s

如果你在測試時發現如下的問題:

Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
    self.run()
  File "./speedtest-cli", line 240, in run
    f = urlopen(self.url, self.data)
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 401, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 419, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1211, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1184, in do_open
    r = h.getresponse(buffering=True)
  File "/usr/lib/python2.7/httplib.py", line 1034, in getresponse
    response.begin()
  File "/usr/lib/python2.7/httplib.py", line 407, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.7/httplib.py", line 371, in _read_status
    raise BadStatusLine(line)
BadStatusLine: ''

......................................Traceback (most recent call last):
  File "./speedtest-cli", line 655, in <module>
:
:
:
:

表示可能speedtest-cli與SpeedTest網站在連線上超過重試次數,請再重新執行命令就能改善,最後再加碼測試一組:

speedtest-cli

danny:~$ ./speedtest-cli --server 2190
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from CHTD, Chunghwa Telecom Co., Ltd. (xxx.xxx.218.xxx)...
Hosted by kbro Co., Ltd. (Pingtung) : 10.967 ms
Testing download speed........................................
Download: 5.99 Mbit/s
Testing upload speed..................................................
Upload: 3.57 Mbit/s