speed_test.py 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright 2012 Matt Martz
  4. # All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  7. # not use this file except in compliance with the License. You may obtain
  8. # a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. # License for the specific language governing permissions and limitations
  16. # under the License.
  17. import csv
  18. import datetime
  19. import errno
  20. import math
  21. import os
  22. import platform
  23. import re
  24. import signal
  25. import socket
  26. import sys
  27. import threading
  28. import timeit
  29. import xml.parsers.expat
  30. try:
  31. import gzip
  32. GZIP_BASE = gzip.GzipFile
  33. except ImportError:
  34. gzip = None
  35. GZIP_BASE = object
  36. __version__ = "2.1.4b1"
  37. class FakeShutdownEvent(object):
  38. """Class to fake a threading.Event.isSet so that users of this module
  39. are not required to register their own threading.Event()
  40. """
  41. @staticmethod
  42. def isSet():
  43. "Dummy method to always return false" ""
  44. return False
  45. is_set = isSet
  46. # Some global variables we use
  47. DEBUG = False
  48. _GLOBAL_DEFAULT_TIMEOUT = object()
  49. PY25PLUS = sys.version_info[:2] >= (2, 5)
  50. PY26PLUS = sys.version_info[:2] >= (2, 6)
  51. PY32PLUS = sys.version_info[:2] >= (3, 2)
  52. PY310PLUS = sys.version_info[:2] >= (3, 10)
  53. # Begin import game to handle Python 2 and Python 3
  54. try:
  55. import json
  56. except ImportError:
  57. try:
  58. import simplejson as json
  59. except ImportError:
  60. json = None
  61. try:
  62. import xml.etree.ElementTree as ET
  63. try:
  64. from xml.etree.ElementTree import _Element as ET_Element
  65. except ImportError:
  66. pass
  67. except ImportError:
  68. from xml.dom import minidom as DOM
  69. from xml.parsers.expat import ExpatError
  70. ET = None
  71. try:
  72. from urllib2 import (
  73. AbstractHTTPHandler,
  74. HTTPDefaultErrorHandler,
  75. HTTPError,
  76. HTTPErrorProcessor,
  77. HTTPRedirectHandler,
  78. OpenerDirector,
  79. ProxyHandler,
  80. Request,
  81. URLError,
  82. urlopen,
  83. )
  84. except ImportError:
  85. from urllib.request import (
  86. AbstractHTTPHandler,
  87. HTTPDefaultErrorHandler,
  88. HTTPError,
  89. HTTPErrorProcessor,
  90. HTTPRedirectHandler,
  91. OpenerDirector,
  92. ProxyHandler,
  93. Request,
  94. URLError,
  95. urlopen,
  96. )
  97. try:
  98. from httplib import BadStatusLine, HTTPConnection
  99. except ImportError:
  100. from http.client import BadStatusLine, HTTPConnection
  101. try:
  102. from httplib import HTTPSConnection
  103. except ImportError:
  104. try:
  105. from http.client import HTTPSConnection
  106. except ImportError:
  107. HTTPSConnection = None
  108. try:
  109. from httplib import FakeSocket
  110. except ImportError:
  111. FakeSocket = None
  112. try:
  113. from Queue import Queue
  114. except ImportError:
  115. from queue import Queue
  116. try:
  117. from urlparse import urlparse
  118. except ImportError:
  119. from urllib.parse import urlparse
  120. try:
  121. from urlparse import parse_qs
  122. except ImportError:
  123. try:
  124. from urllib.parse import parse_qs
  125. except ImportError:
  126. from cgi import parse_qs
  127. try:
  128. from hashlib import md5
  129. except ImportError:
  130. from md5 import md5
  131. try:
  132. from argparse import SUPPRESS as ARG_SUPPRESS, ArgumentParser as ArgParser
  133. PARSER_TYPE_INT = int
  134. PARSER_TYPE_STR = str
  135. PARSER_TYPE_FLOAT = float
  136. except ImportError:
  137. from optparse import SUPPRESS_HELP as ARG_SUPPRESS, OptionParser as ArgParser
  138. PARSER_TYPE_INT = "int"
  139. PARSER_TYPE_STR = "string"
  140. PARSER_TYPE_FLOAT = "float"
  141. try:
  142. from cStringIO import StringIO
  143. BytesIO = None
  144. except ImportError:
  145. try:
  146. from StringIO import StringIO
  147. BytesIO = None
  148. except ImportError:
  149. from io import BytesIO, StringIO
  150. try:
  151. import __builtin__
  152. except ImportError:
  153. import builtins
  154. from io import FileIO, TextIOWrapper
  155. class _Py3Utf8Output(TextIOWrapper):
  156. """UTF-8 encoded wrapper around stdout for py3, to override
  157. ASCII stdout
  158. """
  159. def __init__(self, f, **kwargs):
  160. buf = FileIO(f.fileno(), "w")
  161. super(_Py3Utf8Output, self).__init__(buf, encoding="utf8", errors="strict")
  162. def write(self, s):
  163. super(_Py3Utf8Output, self).write(s)
  164. self.flush()
  165. _py3_print = getattr(builtins, "print")
  166. try:
  167. _py3_utf8_stdout = _Py3Utf8Output(sys.stdout)
  168. _py3_utf8_stderr = _Py3Utf8Output(sys.stderr)
  169. except OSError:
  170. # sys.stdout/sys.stderr is not a compatible stdout/stderr object
  171. # just use it and hope things go ok
  172. _py3_utf8_stdout = sys.stdout
  173. _py3_utf8_stderr = sys.stderr
  174. def to_utf8(v):
  175. """No-op encode to utf-8 for py3"""
  176. return v
  177. def print_(*args, **kwargs):
  178. """Wrapper function for py3 to print, with a utf-8 encoded stdout"""
  179. if kwargs.get("file") == sys.stderr:
  180. kwargs["file"] = _py3_utf8_stderr
  181. else:
  182. kwargs["file"] = kwargs.get("file", _py3_utf8_stdout)
  183. _py3_print(*args, **kwargs)
  184. else:
  185. del __builtin__
  186. def to_utf8(v):
  187. """Encode value to utf-8 if possible for py2"""
  188. try:
  189. return v.encode("utf8", "strict")
  190. except AttributeError:
  191. return v
  192. def print_(*args, **kwargs):
  193. """The new-style print function for Python 2.4 and 2.5.
  194. Taken from https://pypi.python.org/pypi/six/
  195. Modified to set encoding to UTF-8 always, and to flush after write
  196. """
  197. fp = kwargs.pop("file", sys.stdout)
  198. if fp is None:
  199. return
  200. def write(data):
  201. if not isinstance(data, basestring):
  202. data = str(data)
  203. # If the file has an encoding, encode unicode with it.
  204. encoding = "utf8" # Always trust UTF-8 for output
  205. if isinstance(fp, file) and isinstance(data, unicode) and encoding is not None:
  206. errors = getattr(fp, "errors", None)
  207. if errors is None:
  208. errors = "strict"
  209. data = data.encode(encoding, errors)
  210. fp.write(data)
  211. fp.flush()
  212. want_unicode = False
  213. sep = kwargs.pop("sep", None)
  214. if sep is not None:
  215. if isinstance(sep, unicode):
  216. want_unicode = True
  217. elif not isinstance(sep, str):
  218. raise TypeError("sep must be None or a string")
  219. end = kwargs.pop("end", None)
  220. if end is not None:
  221. if isinstance(end, unicode):
  222. want_unicode = True
  223. elif not isinstance(end, str):
  224. raise TypeError("end must be None or a string")
  225. if kwargs:
  226. raise TypeError("invalid keyword arguments to print()")
  227. if not want_unicode:
  228. for arg in args:
  229. if isinstance(arg, unicode):
  230. want_unicode = True
  231. break
  232. if want_unicode:
  233. newline = unicode("\n")
  234. space = unicode(" ")
  235. else:
  236. newline = "\n"
  237. space = " "
  238. if sep is None:
  239. sep = space
  240. if end is None:
  241. end = newline
  242. for i, arg in enumerate(args):
  243. if i:
  244. write(sep)
  245. write(arg)
  246. write(end)
  247. # Exception "constants" to support Python 2 through Python 3
  248. try:
  249. import ssl
  250. try:
  251. CERT_ERROR = (ssl.CertificateError,)
  252. except AttributeError:
  253. CERT_ERROR = tuple()
  254. HTTP_ERRORS = (HTTPError, URLError, socket.error, ssl.SSLError, BadStatusLine) + CERT_ERROR
  255. except ImportError:
  256. ssl = None
  257. HTTP_ERRORS = (HTTPError, URLError, socket.error, BadStatusLine)
  258. if PY32PLUS:
  259. etree_iter = ET.Element.iter
  260. elif PY25PLUS:
  261. etree_iter = ET_Element.getiterator
  262. if PY26PLUS:
  263. thread_is_alive = threading.Thread.is_alive
  264. else:
  265. thread_is_alive = threading.Thread.isAlive
  266. def event_is_set(event):
  267. try:
  268. return event.is_set()
  269. except AttributeError:
  270. return event.isSet()
  271. class SpeedtestException(Exception):
  272. """Base exception for this module"""
  273. class SpeedtestCLIError(SpeedtestException):
  274. """Generic exception for raising errors during CLI operation"""
  275. class SpeedtestHTTPError(SpeedtestException):
  276. """Base HTTP exception for this module"""
  277. class SpeedtestConfigError(SpeedtestException):
  278. """Configuration XML is invalid"""
  279. class SpeedtestServersError(SpeedtestException):
  280. """Servers XML is invalid"""
  281. class ConfigRetrievalError(SpeedtestHTTPError):
  282. """Could not retrieve config.php"""
  283. class ServersRetrievalError(SpeedtestHTTPError):
  284. """Could not retrieve speedtest-servers.php"""
  285. class InvalidServerIDType(SpeedtestException):
  286. """Server ID used for filtering was not an integer"""
  287. class NoMatchedServers(SpeedtestException):
  288. """No servers matched when filtering"""
  289. class SpeedtestMiniConnectFailure(SpeedtestException):
  290. """Could not connect to the provided speedtest mini server"""
  291. class InvalidSpeedtestMiniServer(SpeedtestException):
  292. """Server provided as a speedtest mini server does not actually appear
  293. to be a speedtest mini server
  294. """
  295. class ShareResultsConnectFailure(SpeedtestException):
  296. """Could not connect to speedtest.net API to POST results"""
  297. class ShareResultsSubmitFailure(SpeedtestException):
  298. """Unable to successfully POST results to speedtest.net API after
  299. connection
  300. """
  301. class SpeedtestUploadTimeout(SpeedtestException):
  302. """testlength configuration reached during upload
  303. Used to ensure the upload halts when no additional data should be sent
  304. """
  305. class SpeedtestBestServerFailure(SpeedtestException):
  306. """Unable to determine best server"""
  307. class SpeedtestMissingBestServer(SpeedtestException):
  308. """get_best_server not called or not able to determine best server"""
  309. def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT, source_address=None):
  310. """Connect to *address* and return the socket object.
  311. Convenience function. Connect to *address* (a 2-tuple ``(host,
  312. port)``) and return the socket object. Passing the optional
  313. *timeout* parameter will set the timeout on the socket instance
  314. before attempting to connect. If no *timeout* is supplied, the
  315. global default timeout setting returned by :func:`getdefaulttimeout`
  316. is used. If *source_address* is set it must be a tuple of (host, port)
  317. for the socket to bind as a source address before making the connection.
  318. An host of '' or port 0 tells the OS to use the default.
  319. Largely vendored from Python 2.7, modified to work with Python 2.4
  320. """
  321. host, port = address
  322. err = None
  323. for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
  324. af, socktype, proto, canonname, sa = res
  325. sock = None
  326. try:
  327. sock = socket.socket(af, socktype, proto)
  328. if timeout is not _GLOBAL_DEFAULT_TIMEOUT:
  329. sock.settimeout(float(timeout))
  330. if source_address:
  331. sock.bind(source_address)
  332. sock.connect(sa)
  333. return sock
  334. except socket.error:
  335. err = get_exception()
  336. if sock is not None:
  337. sock.close()
  338. if err is not None:
  339. raise err
  340. else:
  341. raise socket.error("getaddrinfo returns an empty list")
  342. class SpeedtestHTTPConnection(HTTPConnection):
  343. """Custom HTTPConnection to support source_address across
  344. Python 2.4 - Python 3
  345. """
  346. def __init__(self, *args, **kwargs):
  347. source_address = kwargs.pop("source_address", None)
  348. timeout = kwargs.pop("timeout", 10)
  349. self._tunnel_host = None
  350. HTTPConnection.__init__(self, *args, **kwargs)
  351. self.source_address = source_address
  352. self.timeout = timeout
  353. def connect(self):
  354. """Connect to the host and port specified in __init__."""
  355. try:
  356. self.sock = socket.create_connection((self.host, self.port), self.timeout, self.source_address)
  357. except (AttributeError, TypeError):
  358. self.sock = create_connection((self.host, self.port), self.timeout, self.source_address)
  359. if self._tunnel_host:
  360. self._tunnel()
  361. if HTTPSConnection:
  362. class SpeedtestHTTPSConnection(HTTPSConnection):
  363. """Custom HTTPSConnection to support source_address across
  364. Python 2.4 - Python 3
  365. """
  366. default_port = 443
  367. def __init__(self, *args, **kwargs):
  368. source_address = kwargs.pop("source_address", None)
  369. timeout = kwargs.pop("timeout", 10)
  370. self._tunnel_host = None
  371. HTTPSConnection.__init__(self, *args, **kwargs)
  372. self.timeout = timeout
  373. self.source_address = source_address
  374. def connect(self):
  375. "Connect to a host on a given (SSL) port."
  376. try:
  377. self.sock = socket.create_connection((self.host, self.port), self.timeout, self.source_address)
  378. except (AttributeError, TypeError):
  379. self.sock = create_connection((self.host, self.port), self.timeout, self.source_address)
  380. if self._tunnel_host:
  381. self._tunnel()
  382. if ssl:
  383. try:
  384. kwargs = {}
  385. if hasattr(ssl, "SSLContext"):
  386. if self._tunnel_host:
  387. kwargs["server_hostname"] = self._tunnel_host
  388. else:
  389. kwargs["server_hostname"] = self.host
  390. self.sock = self._context.wrap_socket(self.sock, **kwargs)
  391. except AttributeError:
  392. self.sock = ssl.wrap_socket(self.sock)
  393. try:
  394. self.sock.server_hostname = self.host
  395. except AttributeError:
  396. pass
  397. elif FakeSocket:
  398. # Python 2.4/2.5 support
  399. try:
  400. self.sock = FakeSocket(self.sock, socket.ssl(self.sock))
  401. except AttributeError:
  402. raise SpeedtestException("This version of Python does not support HTTPS/SSL " "functionality")
  403. else:
  404. raise SpeedtestException("This version of Python does not support HTTPS/SSL " "functionality")
  405. def _build_connection(connection, source_address, timeout, context=None):
  406. """Cross Python 2.4 - Python 3 callable to build an ``HTTPConnection`` or
  407. ``HTTPSConnection`` with the args we need
  408. Called from ``http(s)_open`` methods of ``SpeedtestHTTPHandler`` or
  409. ``SpeedtestHTTPSHandler``
  410. """
  411. def inner(host, **kwargs):
  412. kwargs.update({"source_address": source_address, "timeout": timeout})
  413. if context:
  414. kwargs["context"] = context
  415. return connection(host, **kwargs)
  416. return inner
  417. class SpeedtestHTTPHandler(AbstractHTTPHandler):
  418. """Custom ``HTTPHandler`` that can build a ``HTTPConnection`` with the
  419. args we need for ``source_address`` and ``timeout``
  420. """
  421. def __init__(self, debuglevel=0, source_address=None, timeout=10):
  422. AbstractHTTPHandler.__init__(self, debuglevel)
  423. self.source_address = source_address
  424. self.timeout = timeout
  425. def http_open(self, req):
  426. return self.do_open(_build_connection(SpeedtestHTTPConnection, self.source_address, self.timeout), req)
  427. http_request = AbstractHTTPHandler.do_request_
  428. class SpeedtestHTTPSHandler(AbstractHTTPHandler):
  429. """Custom ``HTTPSHandler`` that can build a ``HTTPSConnection`` with the
  430. args we need for ``source_address`` and ``timeout``
  431. """
  432. def __init__(self, debuglevel=0, context=None, source_address=None, timeout=10):
  433. AbstractHTTPHandler.__init__(self, debuglevel)
  434. self._context = context
  435. self.source_address = source_address
  436. self.timeout = timeout
  437. def https_open(self, req):
  438. return self.do_open(
  439. _build_connection(
  440. SpeedtestHTTPSConnection,
  441. self.source_address,
  442. self.timeout,
  443. context=self._context,
  444. ),
  445. req,
  446. )
  447. https_request = AbstractHTTPHandler.do_request_
  448. def build_opener(source_address=None, timeout=10):
  449. """Function similar to ``urllib2.build_opener`` that will build
  450. an ``OpenerDirector`` with the explicit handlers we want,
  451. ``source_address`` for binding, ``timeout`` and our custom
  452. `User-Agent`
  453. """
  454. printer("Timeout set to %d" % timeout, debug=True)
  455. if source_address:
  456. source_address_tuple = (source_address, 0)
  457. printer("Binding to source address: %r" % (source_address_tuple,), debug=True)
  458. else:
  459. source_address_tuple = None
  460. handlers = [
  461. ProxyHandler(),
  462. SpeedtestHTTPHandler(source_address=source_address_tuple, timeout=timeout),
  463. SpeedtestHTTPSHandler(source_address=source_address_tuple, timeout=timeout),
  464. HTTPDefaultErrorHandler(),
  465. HTTPRedirectHandler(),
  466. HTTPErrorProcessor(),
  467. ]
  468. opener = OpenerDirector()
  469. opener.addheaders = [("User-agent", build_user_agent())]
  470. for handler in handlers:
  471. opener.add_handler(handler)
  472. return opener
  473. class GzipDecodedResponse(GZIP_BASE):
  474. """A file-like object to decode a response encoded with the gzip
  475. method, as described in RFC 1952.
  476. Largely copied from ``xmlrpclib``/``xmlrpc.client`` and modified
  477. to work for py2.4-py3
  478. """
  479. def __init__(self, response):
  480. # response doesn't support tell() and read(), required by
  481. # GzipFile
  482. if not gzip:
  483. raise SpeedtestHTTPError("HTTP response body is gzip encoded, " "but gzip support is not available")
  484. IO = BytesIO or StringIO
  485. self.io = IO()
  486. while 1:
  487. chunk = response.read(1024)
  488. if len(chunk) == 0:
  489. break
  490. self.io.write(chunk)
  491. self.io.seek(0)
  492. gzip.GzipFile.__init__(self, mode="rb", fileobj=self.io)
  493. def close(self):
  494. try:
  495. gzip.GzipFile.close(self)
  496. finally:
  497. self.io.close()
  498. def get_exception():
  499. """Helper function to work with py2.4-py3 for getting the current
  500. exception in a try/except block
  501. """
  502. return sys.exc_info()[1]
  503. def distance(origin, destination):
  504. """Determine distance between 2 sets of [lat,lon] in km"""
  505. lat1, lon1 = origin
  506. lat2, lon2 = destination
  507. radius = 6371 # km
  508. dlat = math.radians(lat2 - lat1)
  509. dlon = math.radians(lon2 - lon1)
  510. a = math.sin(dlat / 2) * math.sin(dlat / 2) + math.cos(math.radians(lat1)) * math.cos(
  511. math.radians(lat2)
  512. ) * math.sin(dlon / 2) * math.sin(dlon / 2)
  513. c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
  514. d = radius * c
  515. return d
  516. def build_user_agent():
  517. """Build a Mozilla/5.0 compatible User-Agent string"""
  518. ua_tuple = (
  519. "Mozilla/5.0",
  520. "(%s; U; %s; en-us)" % (platform.platform(), platform.architecture()[0]),
  521. "Python/%s" % platform.python_version(),
  522. "(KHTML, like Gecko)",
  523. "speedtest-cli/%s" % __version__,
  524. )
  525. user_agent = " ".join(ua_tuple)
  526. printer("User-Agent: %s" % user_agent, debug=True)
  527. return user_agent
  528. def build_request(url, data=None, headers=None, bump="0", secure=False):
  529. """Build a urllib2 request object
  530. This function automatically adds a User-Agent header to all requests
  531. """
  532. if not headers:
  533. headers = {}
  534. if url[0] == ":":
  535. scheme = ("http", "https")[bool(secure)]
  536. schemed_url = "%s%s" % (scheme, url)
  537. else:
  538. schemed_url = url
  539. if "?" in url:
  540. delim = "&"
  541. else:
  542. delim = "?"
  543. # WHO YOU GONNA CALL? CACHE BUSTERS!
  544. final_url = "%s%sx=%s.%s" % (schemed_url, delim, int(timeit.time.time() * 1000), bump)
  545. headers.update(
  546. {
  547. "Cache-Control": "no-cache",
  548. }
  549. )
  550. printer("%s %s" % (("GET", "POST")[bool(data)], final_url), debug=True)
  551. return Request(final_url, data=data, headers=headers)
  552. def catch_request(request, opener=None):
  553. """Helper function to catch common exceptions encountered when
  554. establishing a connection with a HTTP/HTTPS request
  555. """
  556. if opener:
  557. _open = opener.open
  558. else:
  559. _open = urlopen
  560. try:
  561. uh = _open(request)
  562. if request.get_full_url() != uh.geturl():
  563. printer("Redirected to %s" % uh.geturl(), debug=True)
  564. return uh, False
  565. except HTTP_ERRORS:
  566. e = get_exception()
  567. return None, e
  568. def get_response_stream(response):
  569. """Helper function to return either a Gzip reader if
  570. ``Content-Encoding`` is ``gzip`` otherwise the response itself
  571. """
  572. try:
  573. getheader = response.headers.getheader
  574. except AttributeError:
  575. getheader = response.getheader
  576. if getheader("content-encoding") == "gzip":
  577. return GzipDecodedResponse(response)
  578. return response
  579. def get_attributes_by_tag_name(dom, tag_name):
  580. """Retrieve an attribute from an XML document and return it in a
  581. consistent format
  582. Only used with xml.dom.minidom, which is likely only to be used
  583. with python versions older than 2.5
  584. """
  585. elem = dom.getElementsByTagName(tag_name)[0]
  586. return dict(list(elem.attributes.items()))
  587. def print_dots(shutdown_event):
  588. """Built in callback function used by Thread classes for printing
  589. status
  590. """
  591. def inner(current, total, start=False, end=False):
  592. if event_is_set(shutdown_event):
  593. return
  594. sys.stdout.write(".")
  595. if current + 1 == total and end is True:
  596. sys.stdout.write("\n")
  597. sys.stdout.flush()
  598. return inner
  599. def do_nothing(*args, **kwargs):
  600. pass
  601. class HTTPDownloader(threading.Thread):
  602. """Thread class for retrieving a URL"""
  603. def __init__(self, i, request, start, timeout, opener=None, shutdown_event=None):
  604. threading.Thread.__init__(self)
  605. self.request = request
  606. self.result = [0]
  607. self.starttime = start
  608. self.timeout = timeout
  609. self.i = i
  610. if opener:
  611. self._opener = opener.open
  612. else:
  613. self._opener = urlopen
  614. if shutdown_event:
  615. self._shutdown_event = shutdown_event
  616. else:
  617. self._shutdown_event = FakeShutdownEvent()
  618. def run(self):
  619. try:
  620. if (timeit.default_timer() - self.starttime) <= self.timeout:
  621. f = self._opener(self.request)
  622. while (
  623. not event_is_set(self._shutdown_event) and (timeit.default_timer() - self.starttime) <= self.timeout
  624. ):
  625. self.result.append(len(f.read(10240)))
  626. if self.result[-1] == 0:
  627. break
  628. f.close()
  629. except IOError:
  630. pass
  631. except HTTP_ERRORS:
  632. pass
  633. class HTTPUploaderData(object):
  634. """File like object to improve cutting off the upload once the timeout
  635. has been reached
  636. """
  637. def __init__(self, length, start, timeout, shutdown_event=None):
  638. self.length = length
  639. self.start = start
  640. self.timeout = timeout
  641. if shutdown_event:
  642. self._shutdown_event = shutdown_event
  643. else:
  644. self._shutdown_event = FakeShutdownEvent()
  645. self._data = None
  646. self.total = [0]
  647. def pre_allocate(self):
  648. chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  649. multiplier = int(round(int(self.length) / 36.0))
  650. IO = BytesIO or StringIO
  651. try:
  652. self._data = IO(("content1=%s" % (chars * multiplier)[0 : int(self.length) - 9]).encode())
  653. except MemoryError:
  654. raise SpeedtestCLIError("Insufficient memory to pre-allocate upload data. Please " "use --no-pre-allocate")
  655. @property
  656. def data(self):
  657. if not self._data:
  658. self.pre_allocate()
  659. return self._data
  660. def read(self, n=10240):
  661. if (timeit.default_timer() - self.start) <= self.timeout and not event_is_set(self._shutdown_event):
  662. chunk = self.data.read(n)
  663. self.total.append(len(chunk))
  664. return chunk
  665. else:
  666. raise SpeedtestUploadTimeout()
  667. def __len__(self):
  668. return self.length
  669. class HTTPUploader(threading.Thread):
  670. """Thread class for putting a URL"""
  671. def __init__(self, i, request, start, size, timeout, opener=None, shutdown_event=None):
  672. threading.Thread.__init__(self)
  673. self.request = request
  674. self.request.data.start = self.starttime = start
  675. self.size = size
  676. self.result = 0
  677. self.timeout = timeout
  678. self.i = i
  679. if opener:
  680. self._opener = opener.open
  681. else:
  682. self._opener = urlopen
  683. if shutdown_event:
  684. self._shutdown_event = shutdown_event
  685. else:
  686. self._shutdown_event = FakeShutdownEvent()
  687. def run(self):
  688. request = self.request
  689. try:
  690. if (timeit.default_timer() - self.starttime) <= self.timeout and not event_is_set(self._shutdown_event):
  691. try:
  692. f = self._opener(request)
  693. except TypeError:
  694. # PY24 expects a string or buffer
  695. # This also causes issues with Ctrl-C, but we will concede
  696. # for the moment that Ctrl-C on PY24 isn't immediate
  697. request = build_request(self.request.get_full_url(), data=request.data.read(self.size))
  698. f = self._opener(request)
  699. f.read(11)
  700. f.close()
  701. self.result = sum(self.request.data.total)
  702. else:
  703. self.result = 0
  704. except (IOError, SpeedtestUploadTimeout):
  705. self.result = sum(self.request.data.total)
  706. except HTTP_ERRORS:
  707. self.result = 0
  708. class SpeedtestResults(object):
  709. """Class for holding the results of a speedtest, including:
  710. Download speed
  711. Upload speed
  712. Ping/Latency to test server
  713. Data about server that the test was run against
  714. Additionally this class can return a result data as a dictionary or CSV,
  715. as well as submit a POST of the result data to the speedtest.net API
  716. to get a share results image link.
  717. """
  718. def __init__(self, download=0, upload=0, ping=0, server=None, client=None, opener=None, secure=False):
  719. self.download = download
  720. self.upload = upload
  721. self.ping = ping
  722. if server is None:
  723. self.server = {}
  724. else:
  725. self.server = server
  726. self.client = client or {}
  727. self._share = None
  728. self.timestamp = "%sZ" % datetime.datetime.utcnow().isoformat()
  729. self.bytes_received = 0
  730. self.bytes_sent = 0
  731. if opener:
  732. self._opener = opener
  733. else:
  734. self._opener = build_opener()
  735. self._secure = secure
  736. def __repr__(self):
  737. return repr(self.dict())
  738. def share(self):
  739. """POST data to the speedtest.net API to obtain a share results
  740. link
  741. """
  742. if self._share:
  743. return self._share
  744. download = int(round(self.download / 1000.0, 0))
  745. ping = int(round(self.ping, 0))
  746. upload = int(round(self.upload / 1000.0, 0))
  747. # Build the request to send results back to speedtest.net
  748. # We use a list instead of a dict because the API expects parameters
  749. # in a certain order
  750. api_data = [
  751. "recommendedserverid=%s" % self.server["id"],
  752. "ping=%s" % ping,
  753. "screenresolution=",
  754. "promo=",
  755. "download=%s" % download,
  756. "screendpi=",
  757. "upload=%s" % upload,
  758. "testmethod=http",
  759. "hash=%s" % md5(("%s-%s-%s-%s" % (ping, upload, download, "297aae72")).encode()).hexdigest(),
  760. "touchscreen=none",
  761. "startmode=pingselect",
  762. "accuracy=1",
  763. "bytesreceived=%s" % self.bytes_received,
  764. "bytessent=%s" % self.bytes_sent,
  765. "serverid=%s" % self.server["id"],
  766. ]
  767. headers = {"Referer": "http://c.speedtest.net/flash/speedtest.swf"}
  768. request = build_request(
  769. "://www.speedtest.net/api/api.php", data="&".join(api_data).encode(), headers=headers, secure=self._secure
  770. )
  771. f, e = catch_request(request, opener=self._opener)
  772. if e:
  773. raise ShareResultsConnectFailure(e)
  774. response = f.read()
  775. code = f.code
  776. f.close()
  777. if int(code) != 200:
  778. raise ShareResultsSubmitFailure("Could not submit results to " "speedtest.net")
  779. qsargs = parse_qs(response.decode())
  780. resultid = qsargs.get("resultid")
  781. if not resultid or len(resultid) != 1:
  782. raise ShareResultsSubmitFailure("Could not submit results to " "speedtest.net")
  783. self._share = "http://www.speedtest.net/result/%s.png" % resultid[0]
  784. return self._share
  785. def dict(self):
  786. """Return dictionary of result data"""
  787. return {
  788. "download": self.download,
  789. "upload": self.upload,
  790. "ping": self.ping,
  791. "server": self.server,
  792. "timestamp": self.timestamp,
  793. "bytes_sent": self.bytes_sent,
  794. "bytes_received": self.bytes_received,
  795. "share": self._share,
  796. "client": self.client,
  797. }
  798. @staticmethod
  799. def csv_header(delimiter=","):
  800. """Return CSV Headers"""
  801. row = [
  802. "Server ID",
  803. "Sponsor",
  804. "Server Name",
  805. "Timestamp",
  806. "Distance",
  807. "Ping",
  808. "Download",
  809. "Upload",
  810. "Share",
  811. "IP Address",
  812. ]
  813. out = StringIO()
  814. writer = csv.writer(out, delimiter=delimiter, lineterminator="")
  815. writer.writerow([to_utf8(v) for v in row])
  816. return out.getvalue()
  817. def csv(self, delimiter=","):
  818. """Return data in CSV format"""
  819. data = self.dict()
  820. out = StringIO()
  821. writer = csv.writer(out, delimiter=delimiter, lineterminator="")
  822. row = [
  823. data["server"]["id"],
  824. data["server"]["sponsor"],
  825. data["server"]["name"],
  826. data["timestamp"],
  827. data["server"]["d"],
  828. data["ping"],
  829. data["download"],
  830. data["upload"],
  831. self._share or "",
  832. self.client["ip"],
  833. ]
  834. writer.writerow([to_utf8(v) for v in row])
  835. return out.getvalue()
  836. def json(self, pretty=False):
  837. """Return data in JSON format"""
  838. kwargs = {}
  839. if pretty:
  840. kwargs.update({"indent": 4, "sort_keys": True})
  841. return json.dumps(self.dict(), **kwargs)
  842. class Speedtest(object):
  843. """Class for performing standard speedtest.net testing operations"""
  844. def __init__(self, config=None, source_address=None, timeout=10, secure=False, shutdown_event=None):
  845. self.config = {}
  846. self._source_address = source_address
  847. self._timeout = timeout
  848. self._opener = build_opener(source_address, timeout)
  849. self._secure = secure
  850. if shutdown_event:
  851. self._shutdown_event = shutdown_event
  852. else:
  853. self._shutdown_event = FakeShutdownEvent()
  854. self.get_config()
  855. if config is not None:
  856. self.config.update(config)
  857. self.servers = {}
  858. self.closest = []
  859. self._best = {}
  860. self.results = SpeedtestResults(
  861. client=self.config["client"],
  862. opener=self._opener,
  863. secure=secure,
  864. )
  865. @property
  866. def best(self):
  867. if not self._best:
  868. self.get_best_server()
  869. return self._best
  870. def get_config(self):
  871. """Download the speedtest.net configuration and return only the data
  872. we are interested in
  873. """
  874. headers = {}
  875. if gzip:
  876. headers["Accept-Encoding"] = "gzip"
  877. request = build_request("://www.speedtest.net/speedtest-config.php", headers=headers, secure=self._secure)
  878. uh, e = catch_request(request, opener=self._opener)
  879. if e:
  880. raise ConfigRetrievalError(e)
  881. configxml_list = []
  882. stream = get_response_stream(uh)
  883. while 1:
  884. try:
  885. configxml_list.append(stream.read(1024))
  886. except (OSError, EOFError):
  887. raise ConfigRetrievalError(get_exception())
  888. if len(configxml_list[-1]) == 0:
  889. break
  890. stream.close()
  891. uh.close()
  892. if int(uh.code) != 200:
  893. return None
  894. configxml = "".encode().join(configxml_list)
  895. printer("Config XML:\n%s" % configxml, debug=True)
  896. try:
  897. try:
  898. root = ET.fromstring(configxml)
  899. except ET.ParseError:
  900. e = get_exception()
  901. raise SpeedtestConfigError("Malformed speedtest.net configuration: %s" % e)
  902. server_config = root.find("server-config").attrib
  903. download = root.find("download").attrib
  904. upload = root.find("upload").attrib
  905. # times = root.find('times').attrib
  906. client = root.find("client").attrib
  907. except AttributeError:
  908. try:
  909. root = DOM.parseString(configxml)
  910. except ExpatError:
  911. e = get_exception()
  912. raise SpeedtestConfigError("Malformed speedtest.net configuration: %s" % e)
  913. server_config = get_attributes_by_tag_name(root, "server-config")
  914. download = get_attributes_by_tag_name(root, "download")
  915. upload = get_attributes_by_tag_name(root, "upload")
  916. # times = get_attributes_by_tag_name(root, 'times')
  917. client = get_attributes_by_tag_name(root, "client")
  918. ignore_servers = [int(i) for i in server_config["ignoreids"].split(",") if i]
  919. ratio = int(upload["ratio"])
  920. upload_max = int(upload["maxchunkcount"])
  921. up_sizes = [32768, 65536, 131072, 262144, 524288, 1048576, 7340032]
  922. sizes = {"upload": up_sizes[ratio - 1 :], "download": [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000]}
  923. size_count = len(sizes["upload"])
  924. upload_count = int(math.ceil(upload_max / size_count))
  925. counts = {"upload": upload_count, "download": int(download["threadsperurl"])}
  926. threads = {"upload": int(upload["threads"]), "download": int(server_config["threadcount"]) * 2}
  927. length = {"upload": int(upload["testlength"]), "download": int(download["testlength"])}
  928. self.config.update(
  929. {
  930. "client": client,
  931. "ignore_servers": ignore_servers,
  932. "sizes": sizes,
  933. "counts": counts,
  934. "threads": threads,
  935. "length": length,
  936. "upload_max": upload_count * size_count,
  937. }
  938. )
  939. try:
  940. self.lat_lon = (float(client["lat"]), float(client["lon"]))
  941. except ValueError:
  942. raise SpeedtestConfigError("Unknown location: lat=%r lon=%r" % (client.get("lat"), client.get("lon")))
  943. printer("Config:\n%r" % self.config, debug=True)
  944. return self.config
  945. def get_servers(self, servers=None, exclude=None):
  946. """Retrieve a the list of speedtest.net servers, optionally filtered
  947. to servers matching those specified in the ``servers`` argument
  948. """
  949. if servers is None:
  950. servers = []
  951. if exclude is None:
  952. exclude = []
  953. self.servers.clear()
  954. for server_list in (servers, exclude):
  955. for i, s in enumerate(server_list):
  956. try:
  957. server_list[i] = int(s)
  958. except ValueError:
  959. raise InvalidServerIDType("%s is an invalid server type, must be int" % s)
  960. urls = [
  961. "://www.speedtest.net/speedtest-servers-static.php",
  962. "http://c.speedtest.net/speedtest-servers-static.php",
  963. "://www.speedtest.net/speedtest-servers.php",
  964. "http://c.speedtest.net/speedtest-servers.php",
  965. ]
  966. headers = {}
  967. if gzip:
  968. headers["Accept-Encoding"] = "gzip"
  969. errors = []
  970. for url in urls:
  971. try:
  972. request = build_request(
  973. "%s?threads=%s" % (url, self.config["threads"]["download"]), headers=headers, secure=self._secure
  974. )
  975. uh, e = catch_request(request, opener=self._opener)
  976. if e:
  977. errors.append("%s" % e)
  978. raise ServersRetrievalError()
  979. stream = get_response_stream(uh)
  980. serversxml_list = []
  981. while 1:
  982. try:
  983. serversxml_list.append(stream.read(1024))
  984. except (OSError, EOFError):
  985. raise ServersRetrievalError(get_exception())
  986. if len(serversxml_list[-1]) == 0:
  987. break
  988. stream.close()
  989. uh.close()
  990. if int(uh.code) != 200:
  991. raise ServersRetrievalError()
  992. serversxml = "".encode().join(serversxml_list)
  993. printer("Servers XML:\n%s" % serversxml, debug=True)
  994. try:
  995. try:
  996. try:
  997. root = ET.fromstring(serversxml)
  998. except ET.ParseError:
  999. e = get_exception()
  1000. raise SpeedtestServersError("Malformed speedtest.net server list: %s" % e)
  1001. elements = etree_iter(root, "server")
  1002. except AttributeError:
  1003. try:
  1004. root = DOM.parseString(serversxml)
  1005. except ExpatError:
  1006. e = get_exception()
  1007. raise SpeedtestServersError("Malformed speedtest.net server list: %s" % e)
  1008. elements = root.getElementsByTagName("server")
  1009. except (SyntaxError, xml.parsers.expat.ExpatError):
  1010. raise ServersRetrievalError()
  1011. for server in elements:
  1012. try:
  1013. attrib = server.attrib
  1014. except AttributeError:
  1015. attrib = dict(list(server.attributes.items()))
  1016. if servers and int(attrib.get("id")) not in servers:
  1017. continue
  1018. if int(attrib.get("id")) in self.config["ignore_servers"] or int(attrib.get("id")) in exclude:
  1019. continue
  1020. try:
  1021. d = distance(self.lat_lon, (float(attrib.get("lat")), float(attrib.get("lon"))))
  1022. except Exception:
  1023. continue
  1024. attrib["d"] = d
  1025. try:
  1026. self.servers[d].append(attrib)
  1027. except KeyError:
  1028. self.servers[d] = [attrib]
  1029. break
  1030. except ServersRetrievalError:
  1031. continue
  1032. if (servers or exclude) and not self.servers:
  1033. raise NoMatchedServers()
  1034. return self.servers
  1035. def set_mini_server(self, server):
  1036. """Instead of querying for a list of servers, set a link to a
  1037. speedtest mini server
  1038. """
  1039. urlparts = urlparse(server)
  1040. name, ext = os.path.splitext(urlparts[2])
  1041. if ext:
  1042. url = os.path.dirname(server)
  1043. else:
  1044. url = server
  1045. request = build_request(url)
  1046. uh, e = catch_request(request, opener=self._opener)
  1047. if e:
  1048. raise SpeedtestMiniConnectFailure("Failed to connect to %s" % server)
  1049. else:
  1050. text = uh.read()
  1051. uh.close()
  1052. extension = re.findall('upload_?[Ee]xtension: "([^"]+)"', text.decode())
  1053. if not extension:
  1054. for ext in ["php", "asp", "aspx", "jsp"]:
  1055. try:
  1056. f = self._opener.open("%s/speedtest/upload.%s" % (url, ext))
  1057. except Exception:
  1058. pass
  1059. else:
  1060. data = f.read().strip().decode()
  1061. if f.code == 200 and len(data.splitlines()) == 1 and re.match("size=[0-9]", data):
  1062. extension = [ext]
  1063. break
  1064. if not urlparts or not extension:
  1065. raise InvalidSpeedtestMiniServer("Invalid Speedtest Mini Server: " "%s" % server)
  1066. self.servers = [
  1067. {
  1068. "sponsor": "Speedtest Mini",
  1069. "name": urlparts[1],
  1070. "d": 0,
  1071. "url": "%s/speedtest/upload.%s" % (url.rstrip("/"), extension[0]),
  1072. "latency": 0,
  1073. "id": 0,
  1074. }
  1075. ]
  1076. return self.servers
  1077. def get_closest_servers(self, limit=5):
  1078. """Limit servers to the closest speedtest.net servers based on
  1079. geographic distance
  1080. """
  1081. if not self.servers:
  1082. self.get_servers()
  1083. for d in sorted(self.servers.keys()):
  1084. for s in self.servers[d]:
  1085. self.closest.append(s)
  1086. if len(self.closest) == limit:
  1087. break
  1088. else:
  1089. continue
  1090. break
  1091. printer("Closest Servers:\n%r" % self.closest, debug=True)
  1092. return self.closest
  1093. def get_best_server(self, servers=None):
  1094. """Perform a speedtest.net "ping" to determine which speedtest.net
  1095. server has the lowest latency
  1096. """
  1097. if not servers:
  1098. if not self.closest:
  1099. servers = self.get_closest_servers()
  1100. servers = self.closest
  1101. if self._source_address:
  1102. source_address_tuple = (self._source_address, 0)
  1103. else:
  1104. source_address_tuple = None
  1105. user_agent = build_user_agent()
  1106. results = {}
  1107. for server in servers:
  1108. cum = []
  1109. url = os.path.dirname(server["url"])
  1110. stamp = int(timeit.time.time() * 1000)
  1111. latency_url = "%s/latency.txt?x=%s" % (url, stamp)
  1112. for i in range(0, 3):
  1113. this_latency_url = "%s.%s" % (latency_url, i)
  1114. printer("%s %s" % ("GET", this_latency_url), debug=True)
  1115. urlparts = urlparse(latency_url)
  1116. try:
  1117. if urlparts[0] == "https":
  1118. h = SpeedtestHTTPSConnection(urlparts[1], source_address=source_address_tuple)
  1119. else:
  1120. h = SpeedtestHTTPConnection(urlparts[1], source_address=source_address_tuple)
  1121. headers = {"User-Agent": user_agent}
  1122. path = "%s?%s" % (urlparts[2], urlparts[4])
  1123. start = timeit.default_timer()
  1124. h.request("GET", path, headers=headers)
  1125. r = h.getresponse()
  1126. total = timeit.default_timer() - start
  1127. except HTTP_ERRORS:
  1128. e = get_exception()
  1129. printer("ERROR: %r" % e, debug=True)
  1130. cum.append(3600)
  1131. continue
  1132. text = r.read(9)
  1133. if int(r.status) == 200 and text == "test=test".encode():
  1134. cum.append(total)
  1135. else:
  1136. cum.append(3600)
  1137. h.close()
  1138. avg = round((sum(cum) / 6) * 1000.0, 3)
  1139. results[avg] = server
  1140. try:
  1141. fastest = sorted(results.keys())[0]
  1142. except IndexError:
  1143. raise SpeedtestBestServerFailure("Unable to connect to servers to " "test latency.")
  1144. best = results[fastest]
  1145. best["latency"] = fastest
  1146. self.results.ping = fastest
  1147. self.results.server = best
  1148. self._best.update(best)
  1149. printer("Best Server:\n%r" % best, debug=True)
  1150. return best
  1151. def download(self, callback=do_nothing, threads=None):
  1152. """Test download speed against speedtest.net
  1153. A ``threads`` value of ``None`` will fall back to those dictated
  1154. by the speedtest.net configuration
  1155. """
  1156. urls = []
  1157. for size in self.config["sizes"]["download"]:
  1158. for _ in range(0, self.config["counts"]["download"]):
  1159. urls.append("%s/random%sx%s.jpg" % (os.path.dirname(self.best["url"]), size, size))
  1160. request_count = len(urls)
  1161. requests = []
  1162. for i, url in enumerate(urls):
  1163. requests.append(build_request(url, bump=i, secure=self._secure))
  1164. max_threads = threads or self.config["threads"]["download"]
  1165. in_flight = {"threads": 0}
  1166. def producer(q, requests, request_count):
  1167. for i, request in enumerate(requests):
  1168. thread = HTTPDownloader(
  1169. i,
  1170. request,
  1171. start,
  1172. self.config["length"]["download"],
  1173. opener=self._opener,
  1174. shutdown_event=self._shutdown_event,
  1175. )
  1176. while in_flight["threads"] >= max_threads:
  1177. timeit.time.sleep(0.001)
  1178. thread.start()
  1179. q.put(thread, True)
  1180. in_flight["threads"] += 1
  1181. callback(i, request_count, start=True)
  1182. finished = []
  1183. def consumer(q, request_count):
  1184. _is_alive = thread_is_alive
  1185. while len(finished) < request_count:
  1186. thread = q.get(True)
  1187. while _is_alive(thread):
  1188. thread.join(timeout=0.001)
  1189. in_flight["threads"] -= 1
  1190. finished.append(sum(thread.result))
  1191. callback(thread.i, request_count, end=True)
  1192. q = Queue(max_threads)
  1193. prod_thread = threading.Thread(target=producer, args=(q, requests, request_count))
  1194. cons_thread = threading.Thread(target=consumer, args=(q, request_count))
  1195. start = timeit.default_timer()
  1196. prod_thread.start()
  1197. cons_thread.start()
  1198. _is_alive = thread_is_alive
  1199. while _is_alive(prod_thread):
  1200. prod_thread.join(timeout=0.001)
  1201. while _is_alive(cons_thread):
  1202. cons_thread.join(timeout=0.001)
  1203. stop = timeit.default_timer()
  1204. self.results.bytes_received = sum(finished)
  1205. self.results.download = (self.results.bytes_received / (stop - start)) * 8.0
  1206. if self.results.download > 100000:
  1207. self.config["threads"]["upload"] = 8
  1208. return self.results.download
  1209. def upload(self, callback=do_nothing, pre_allocate=True, threads=None):
  1210. """Test upload speed against speedtest.net
  1211. A ``threads`` value of ``None`` will fall back to those dictated
  1212. by the speedtest.net configuration
  1213. """
  1214. sizes = []
  1215. for size in self.config["sizes"]["upload"]:
  1216. for _ in range(0, self.config["counts"]["upload"]):
  1217. sizes.append(size)
  1218. # request_count = len(sizes)
  1219. request_count = self.config["upload_max"]
  1220. requests = []
  1221. for i, size in enumerate(sizes):
  1222. # We set ``0`` for ``start`` and handle setting the actual
  1223. # ``start`` in ``HTTPUploader`` to get better measurements
  1224. data = HTTPUploaderData(size, 0, self.config["length"]["upload"], shutdown_event=self._shutdown_event)
  1225. if pre_allocate:
  1226. data.pre_allocate()
  1227. headers = {"Content-length": size}
  1228. requests.append((build_request(self.best["url"], data, secure=self._secure, headers=headers), size))
  1229. max_threads = threads or self.config["threads"]["upload"]
  1230. in_flight = {"threads": 0}
  1231. def producer(q, requests, request_count):
  1232. for i, request in enumerate(requests[:request_count]):
  1233. thread = HTTPUploader(
  1234. i,
  1235. request[0],
  1236. start,
  1237. request[1],
  1238. self.config["length"]["upload"],
  1239. opener=self._opener,
  1240. shutdown_event=self._shutdown_event,
  1241. )
  1242. while in_flight["threads"] >= max_threads:
  1243. timeit.time.sleep(0.001)
  1244. thread.start()
  1245. q.put(thread, True)
  1246. in_flight["threads"] += 1
  1247. callback(i, request_count, start=True)
  1248. finished = []
  1249. def consumer(q, request_count):
  1250. _is_alive = thread_is_alive
  1251. while len(finished) < request_count:
  1252. thread = q.get(True)
  1253. while _is_alive(thread):
  1254. thread.join(timeout=0.001)
  1255. in_flight["threads"] -= 1
  1256. finished.append(thread.result)
  1257. callback(thread.i, request_count, end=True)
  1258. q = Queue(threads or self.config["threads"]["upload"])
  1259. prod_thread = threading.Thread(target=producer, args=(q, requests, request_count))
  1260. cons_thread = threading.Thread(target=consumer, args=(q, request_count))
  1261. start = timeit.default_timer()
  1262. prod_thread.start()
  1263. cons_thread.start()
  1264. _is_alive = thread_is_alive
  1265. while _is_alive(prod_thread):
  1266. prod_thread.join(timeout=0.1)
  1267. while _is_alive(cons_thread):
  1268. cons_thread.join(timeout=0.1)
  1269. stop = timeit.default_timer()
  1270. self.results.bytes_sent = sum(finished)
  1271. self.results.upload = (self.results.bytes_sent / (stop - start)) * 8.0
  1272. return self.results.upload
  1273. def ctrl_c(shutdown_event):
  1274. """Catch Ctrl-C key sequence and set a SHUTDOWN_EVENT for our threaded
  1275. operations
  1276. """
  1277. def inner(signum, frame):
  1278. shutdown_event.set()
  1279. printer("\nCancelling...", error=True)
  1280. sys.exit(0)
  1281. return inner
  1282. def version():
  1283. """Print the version"""
  1284. printer("speedtest-cli %s" % __version__)
  1285. printer("Python %s" % sys.version.replace("\n", ""))
  1286. sys.exit(0)
  1287. def csv_header(delimiter=","):
  1288. """Print the CSV Headers"""
  1289. printer(SpeedtestResults.csv_header(delimiter=delimiter))
  1290. sys.exit(0)
  1291. def parse_args():
  1292. """Function to handle building and parsing of command line arguments"""
  1293. description = (
  1294. "Command line interface for testing internet bandwidth using "
  1295. "speedtest.net.\n"
  1296. "------------------------------------------------------------"
  1297. "--------------\n"
  1298. "https://github.com/sivel/speedtest-cli"
  1299. )
  1300. parser = ArgParser(description=description)
  1301. # Give optparse.OptionParser an `add_argument` method for
  1302. # compatibility with argparse.ArgumentParser
  1303. try:
  1304. parser.add_argument = parser.add_option
  1305. except AttributeError:
  1306. pass
  1307. parser.add_argument(
  1308. "--no-download",
  1309. dest="download",
  1310. default=True,
  1311. action="store_const",
  1312. const=False,
  1313. help="Do not perform download test",
  1314. )
  1315. parser.add_argument(
  1316. "--no-upload", dest="upload", default=True, action="store_const", const=False, help="Do not perform upload test"
  1317. )
  1318. parser.add_argument(
  1319. "--single",
  1320. default=False,
  1321. action="store_true",
  1322. help="Only use a single connection instead of " "multiple. This simulates a typical file " "transfer.",
  1323. )
  1324. parser.add_argument(
  1325. "--bytes",
  1326. dest="units",
  1327. action="store_const",
  1328. const=("byte", 8),
  1329. default=("bit", 1),
  1330. help="Display values in bytes instead of bits. Does "
  1331. "not affect the image generated by --share, nor "
  1332. "output from --json or --csv",
  1333. )
  1334. parser.add_argument(
  1335. "--share",
  1336. action="store_true",
  1337. help="Generate and provide a URL to the speedtest.net " "share results image, not displayed with --csv",
  1338. )
  1339. parser.add_argument(
  1340. "--simple", action="store_true", default=False, help="Suppress verbose output, only show basic " "information"
  1341. )
  1342. parser.add_argument(
  1343. "--csv",
  1344. action="store_true",
  1345. default=False,
  1346. help="Suppress verbose output, only show basic "
  1347. "information in CSV format. Speeds listed in "
  1348. "bit/s and not affected by --bytes",
  1349. )
  1350. parser.add_argument(
  1351. "--csv-delimiter",
  1352. default=",",
  1353. type=PARSER_TYPE_STR,
  1354. help="Single character delimiter to use in CSV " 'output. Default ","',
  1355. )
  1356. parser.add_argument("--csv-header", action="store_true", default=False, help="Print CSV headers")
  1357. parser.add_argument(
  1358. "--json",
  1359. action="store_true",
  1360. default=False,
  1361. help="Suppress verbose output, only show basic "
  1362. "information in JSON format. Speeds listed in "
  1363. "bit/s and not affected by --bytes",
  1364. )
  1365. parser.add_argument(
  1366. "--list", action="store_true", help="Display a list of speedtest.net servers " "sorted by distance"
  1367. )
  1368. parser.add_argument(
  1369. "--server",
  1370. type=PARSER_TYPE_INT,
  1371. action="append",
  1372. help="Specify a server ID to test against. Can be " "supplied multiple times",
  1373. )
  1374. parser.add_argument(
  1375. "--exclude",
  1376. type=PARSER_TYPE_INT,
  1377. action="append",
  1378. help="Exclude a server from selection. Can be " "supplied multiple times",
  1379. )
  1380. parser.add_argument("--mini", help="URL of the Speedtest Mini server")
  1381. parser.add_argument("--source", help="Source IP address to bind to")
  1382. parser.add_argument("--timeout", default=10, type=PARSER_TYPE_FLOAT, help="HTTP timeout in seconds. Default 10")
  1383. parser.add_argument(
  1384. "--secure",
  1385. action="store_true",
  1386. help="Use HTTPS instead of HTTP when communicating " "with speedtest.net operated servers",
  1387. )
  1388. parser.add_argument(
  1389. "--no-pre-allocate",
  1390. dest="pre_allocate",
  1391. action="store_const",
  1392. default=True,
  1393. const=False,
  1394. help="Do not pre allocate upload data. Pre allocation "
  1395. "is enabled by default to improve upload "
  1396. "performance. To support systems with "
  1397. "insufficient memory, use this option to avoid a "
  1398. "MemoryError",
  1399. )
  1400. parser.add_argument("--version", action="store_true", help="Show the version number and exit")
  1401. parser.add_argument("--debug", action="store_true", help=ARG_SUPPRESS, default=ARG_SUPPRESS)
  1402. options = parser.parse_args()
  1403. if isinstance(options, tuple):
  1404. args = options[0]
  1405. else:
  1406. args = options
  1407. return args
  1408. def validate_optional_args(args):
  1409. """Check if an argument was provided that depends on a module that may
  1410. not be part of the Python standard library.
  1411. If such an argument is supplied, and the module does not exist, exit
  1412. with an error stating which module is missing.
  1413. """
  1414. optional_args = {
  1415. "json": ("json/simplejson python module", json),
  1416. "secure": ("SSL support", HTTPSConnection),
  1417. }
  1418. for arg, info in optional_args.items():
  1419. if getattr(args, arg, False) and info[1] is None:
  1420. raise SystemExit("%s is not installed. --%s is " "unavailable" % (info[0], arg))
  1421. def printer(string, quiet=False, debug=False, error=False, **kwargs):
  1422. """Helper function print a string with various features"""
  1423. if debug and not DEBUG:
  1424. return
  1425. if debug:
  1426. if sys.stdout.isatty():
  1427. out = "\033[1;30mDEBUG: %s\033[0m" % string
  1428. else:
  1429. out = "DEBUG: %s" % string
  1430. else:
  1431. out = string
  1432. if error:
  1433. kwargs["file"] = sys.stderr
  1434. if not quiet:
  1435. print_(out, **kwargs)
  1436. def shell():
  1437. """Run the full speedtest.net test"""
  1438. global DEBUG
  1439. shutdown_event = threading.Event()
  1440. signal.signal(signal.SIGINT, ctrl_c(shutdown_event))
  1441. args = parse_args()
  1442. # Print the version and exit
  1443. if args.version:
  1444. version()
  1445. if not args.download and not args.upload:
  1446. raise SpeedtestCLIError("Cannot supply both --no-download and " "--no-upload")
  1447. if len(args.csv_delimiter) != 1:
  1448. raise SpeedtestCLIError("--csv-delimiter must be a single character")
  1449. if args.csv_header:
  1450. csv_header(args.csv_delimiter)
  1451. validate_optional_args(args)
  1452. debug = getattr(args, "debug", False)
  1453. if debug == "SUPPRESSHELP":
  1454. debug = False
  1455. if debug:
  1456. DEBUG = True
  1457. if args.simple or args.csv or args.json:
  1458. quiet = True
  1459. else:
  1460. quiet = False
  1461. if args.csv or args.json:
  1462. machine_format = True
  1463. else:
  1464. machine_format = False
  1465. # Don't set a callback if we are running quietly
  1466. if quiet or debug:
  1467. callback = do_nothing
  1468. else:
  1469. callback = print_dots(shutdown_event)
  1470. printer("Retrieving speedtest.net configuration...", quiet)
  1471. try:
  1472. speedtest = Speedtest(source_address=args.source, timeout=args.timeout, secure=args.secure)
  1473. except (ConfigRetrievalError,) + HTTP_ERRORS:
  1474. printer("Cannot retrieve speedtest configuration", error=True)
  1475. raise SpeedtestCLIError(get_exception())
  1476. if args.list:
  1477. try:
  1478. speedtest.get_servers()
  1479. except (ServersRetrievalError,) + HTTP_ERRORS:
  1480. printer("Cannot retrieve speedtest server list", error=True)
  1481. raise SpeedtestCLIError(get_exception())
  1482. for _, servers in sorted(speedtest.servers.items()):
  1483. for server in servers:
  1484. line = "%(id)5s) %(sponsor)s (%(name)s, %(country)s) " "[%(d)0.2f km]" % server
  1485. try:
  1486. printer(line)
  1487. except IOError:
  1488. e = get_exception()
  1489. if e.errno != errno.EPIPE:
  1490. raise
  1491. sys.exit(0)
  1492. printer("Testing from %(isp)s (%(ip)s)..." % speedtest.config["client"], quiet)
  1493. if not args.mini:
  1494. printer("Retrieving speedtest.net server list...", quiet)
  1495. try:
  1496. speedtest.get_servers(servers=args.server, exclude=args.exclude)
  1497. except NoMatchedServers:
  1498. raise SpeedtestCLIError("No matched servers: %s" % ", ".join("%s" % s for s in args.server))
  1499. except (ServersRetrievalError,) + HTTP_ERRORS:
  1500. printer("Cannot retrieve speedtest server list", error=True)
  1501. raise SpeedtestCLIError(get_exception())
  1502. except InvalidServerIDType:
  1503. raise SpeedtestCLIError(
  1504. "%s is an invalid server type, must " "be an int" % ", ".join("%s" % s for s in args.server)
  1505. )
  1506. if args.server and len(args.server) == 1:
  1507. printer("Retrieving information for the selected server...", quiet)
  1508. else:
  1509. printer("Selecting best server based on ping...", quiet)
  1510. speedtest.get_best_server()
  1511. elif args.mini:
  1512. speedtest.get_best_server(speedtest.set_mini_server(args.mini))
  1513. results = speedtest.results
  1514. printer("Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: " "%(latency)s ms" % results.server, quiet)
  1515. if args.download:
  1516. printer("Testing download speed", quiet, end=("", "\n")[bool(debug)])
  1517. speedtest.download(callback=callback, threads=(None, 1)[args.single])
  1518. printer("Download: %0.2f M%s/s" % ((results.download / 1000.0 / 1000.0) / args.units[1], args.units[0]), quiet)
  1519. else:
  1520. printer("Skipping download test", quiet)
  1521. if args.upload:
  1522. printer("Testing upload speed", quiet, end=("", "\n")[bool(debug)])
  1523. speedtest.upload(callback=callback, pre_allocate=args.pre_allocate, threads=(None, 1)[args.single])
  1524. printer("Upload: %0.2f M%s/s" % ((results.upload / 1000.0 / 1000.0) / args.units[1], args.units[0]), quiet)
  1525. else:
  1526. printer("Skipping upload test", quiet)
  1527. printer("Results:\n%r" % results.dict(), debug=True)
  1528. if not args.simple and args.share:
  1529. results.share()
  1530. if args.simple:
  1531. printer(
  1532. "Ping: %s ms\nDownload: %0.2f M%s/s\nUpload: %0.2f M%s/s"
  1533. % (
  1534. results.ping,
  1535. (results.download / 1000.0 / 1000.0) / args.units[1],
  1536. args.units[0],
  1537. (results.upload / 1000.0 / 1000.0) / args.units[1],
  1538. args.units[0],
  1539. )
  1540. )
  1541. elif args.csv:
  1542. printer(results.csv(delimiter=args.csv_delimiter))
  1543. elif args.json:
  1544. printer(results.json())
  1545. if args.share and not machine_format:
  1546. printer("Share results: %s" % results.share())
  1547. def main():
  1548. try:
  1549. shell()
  1550. except KeyboardInterrupt:
  1551. printer("\nCancelling...", error=True)
  1552. except (SpeedtestException, SystemExit):
  1553. e = get_exception()
  1554. # Ignore a successful exit, or argparse exit
  1555. if getattr(e, "code", 1) not in (0, 2):
  1556. msg = "%s" % e
  1557. if not msg:
  1558. msg = "%r" % e
  1559. raise SystemExit("ERROR: %s" % msg)
  1560. if __name__ == "__main__":
  1561. main()