命令列下测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