コード例 #1
0
ファイル: gatso.py プロジェクト: dewet22/gatso
def run_speedtest() -> (dict, float, float):
    st.shutdown_event = threading.Event()
    socket.setdefaulttimeout(SOCKET_TIMEOUT)
    st.build_user_agent()
    config = getConfig()
    if not config:
        return {}, -1, -1
    servers = st.closestServers(config['client'])
    best = getBestServer(servers, st.build_user_agent())
    # print(best)

    urls = []
    for size in [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000]:
        for i in range(0, 4):
            urls.append('%s/random%sx%s.jpg' % (os.path.dirname(best['url']), size, size))
    dlspeed = st.downloadSpeed(urls, True) * 8 / 1024 / 1024
    # print(dlspeed)

    sizes = []
    for size in [int(.25 * 1024 * 1024), int(.5 * 1024 * 1024)]:
        for i in range(0, 25):
            sizes.append(size)
    ulspeed = st.uploadSpeed(best['url'], sizes, True) * 8 / 1024 / 1024
    # print(ulspeed)

    return best, dlspeed, ulspeed
コード例 #2
0
ファイル: probing.py プロジェクト: rubienr/network-monitoring
    def probe(self):
        self.logger.info("starting %s probe " % (type(self).__name__))

        speedtest.shutdown_event = threading.Event()
        config = speedtest.getConfig()
        closestServer = speedtest.closestServers(config['client'])

        best = None
        serverId = self.probeConfig.serverId
        if serverId == None:
            best = speedtest.getBestServer(closestServer)
        else:
            serverId = "%s" % serverId
            self.logger.debug("use server id [%s]" %(serverId))
            best = speedtest.getBestServer(filter(lambda x: x['id'] == serverId, closestServer))
            self.logger.debug("found host for [%s] -> host [%s]" %(serverId, best["host"]))

        transferred, speed, result = None, None, None
        if not "download" in self.probeConfig.direction:
            sizesizes = [int(.25 * 1000 * 1000), int(.5 * 1000 * 1000)]
            sizes = []
            for size in sizesizes:
                for i in range(0, 25):
                    sizes.append(size)
            result = TransferTestResult(units="bit", transferStart=timezone.now(), direction="upload",
                                        probeName=self.probeConfig.probeName)
            transferred, speed = self.uploadSpeed(best['url'], sizes, quiet=True)
            result.transferEnd = timezone.now()
            self.logger.debug('upload: %0.2f M%s/s' % ((speed / 1000 / 1000) * 8, "bit"))

        else:
            sizes = [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000]
            urls = []
            for size in sizes:
                for i in range(0, 4):
                    urls.append('%s/random%sx%s.jpg' % (os.path.dirname(best['url']), size, size))

            result = TransferTestResult(units="bit", transferStart=timezone.now(), direction="download",
                                        probeName=self.probeConfig.probeName)
            transferred, speed = self.downloadSpeed(urls, quiet=True)
            result.transferEnd = timezone.now()
            self.logger.debug('download: %0.2f M%s/s' % ((speed / 1000 / 1000) * 8, "bit"))

        result.interfaceIp = getLocalIp("speedtest.net")
        result.transferredUnits = transferred
        result.transferredUnitsPerSecond = speed * 8
        result.host = best["host"]
        result.save()
        self.appendServerInfos(result, best)
        self.logger.info("%s probe done" % (type(self).__name__))
コード例 #3
0
ファイル: views.py プロジェクト: rubienr/network-monitoring
def getClosestServersView(request):
    config = speedtest.getConfig()
    closestServers = speedtest.closestServers(config['client'])

    # store to db
    models = []
    localIp = getLocalIp("speedtest.net")
    for server in closestServers:
        server["serverId"] = server.pop("id")
        model = SpeedtestServer().fromDict(**server)
        model.interfaceIp = localIp
        models.append(model)
    SpeedtestServer.objects.bulk_create(models)

    # filter/reorder/translate values for view
    title = "Speedtest.net - Closest Server"
    columnToName = OrderedDict([
        ("serverId", "ID"),
        ("name", "City"),
        ("url", "URL"),
        ("country", "Country"),
        ("d", "Distance [km]"),
        #("cc", "country code"),
        #("host", "host name"),
        ("sponsor", ""),
        #("url2", "url"),
        ("lat", "Latitude"),
        ("lon", "Longitude"),
    ])
    columns = columnToName.keys()

    servers = []
    for c in closestServers:
        server = OrderedDict([(columnToName[filteredColumn], c[filteredColumn])
                              for filteredColumn in columns])
        distanceColumn = columnToName["d"]
        server[distanceColumn] = round(server[distanceColumn], 1)
        servers.append(server)

    data = {
        "title": title,
        "tableHeader": servers[0].keys(),
        "servers": servers,
    }

    return render_to_response('bootstrap/serverlist.html',
                              data,
                              context_instance=RequestContext(request))
コード例 #4
0
def main():
    speedtest_cli.shutdown_event = threading.Event()

    parser = argparse.ArgumentParser()
    parser.add_argument('--timeout', default=10, type=int,
                        help='HTTP timeout in seconds. Default 10')
    parser.add_argument('--secure', action='store_true',
                        help='Use HTTPS instead of HTTP when communicating '
                             'with speedtest.net operated servers')
    parser.add_argument('--graphite-host', type=str, default='localhost',
                        help='The graphite host to publish results to')
    parser.add_argument('--graphite-port', type=int, default=2003,
                        help='The graphite (line-receiver) port to publish results to')
    parser.add_argument('--graphite-prefix', type=str, default="speedtest",
                        help='The graphite metrics prefix')
    args = parser.parse_args()

    socket.setdefaulttimeout(args.timeout)
    speedtest_cli.build_user_agent()

    if args.secure:
        speedtest_cli.scheme = 'https'

    try:
        config = speedtest_cli.getConfig()
    except speedtest_cli.URLError:
        print('Cannot retrieve speedtest configuration')
        sys.exit(1)

    servers = speedtest_cli.closestServers(config['client'])
    best = speedtest_cli.getBestServer(servers)

    print("Using speedtest.net server hosted by {sponsor} ({name}).\n"
          "Host: {host}\nDistance: {d:0.1f} km\nLatency: {latency} ms".format(**best))

    sizes = [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000]
    urls = []
    for size in sizes:
        for i in range(0, 4):
            urls.append('%s/random%sx%s.jpg' %
                        (os.path.dirname(best['url']), size, size))
    dlspeed = speedtest_cli.downloadSpeed(urls, True)
    print('Download: %0.2f Mbit/s' % ((dlspeed / 1000 / 1000) * 8))

    sizesizes = [int(.25 * 1000 * 1000), int(.5 * 1000 * 1000)]
    sizes = []
    for size in sizesizes:
        for i in range(0, 25):
            sizes.append(size)
    ulspeed = speedtest_cli.uploadSpeed(best['url'], sizes, True)
    print('Upload: %0.2f Mbit/s' % ((ulspeed/ 1000 / 1000) * 8))

    t = int(time.time())
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((args.graphite_host, args.graphite_port))
    s.send(
        "{prefix}.latency {latency} {timestamp}\n".format(
        prefix=args.graphite_prefix,
        latency=best['latency'],
        timestamp=t,
    ))
    s.send(
        "{prefix}.distance {distance} {timestamp}\n".format(
        prefix=args.graphite_prefix,
        distance=best['d'],
        timestamp=t,
    ))
    s.send(
        "{prefix}.upload {upload} {timestamp}\n".format(
        prefix=args.graphite_prefix,
        upload=ulspeed,
        timestamp=t,
    ))
    s.send(
        "{prefix}.download {download} {timestamp}\n".format(
        prefix=args.graphite_prefix,
        download=dlspeed,
        timestamp=t,
    ))
    s.close()
コード例 #5
0
    def run(self):

        # since speedtest_cli likes to print crap, we need to squelch it
        @contextlib.contextmanager
        def nostdout():
            save_stdout = sys.stdout
            sys.stdout = StringIO()
            yield
            sys.stdout = save_stdout

        if not self.url:
            with nostdout():
                try:
                    config = speedtest_cli.getConfig()
                    servers = speedtest_cli.closestServers(config['client'])
                    best = speedtest_cli.getBestServer(servers)
                    # 1500x1500 is about 4.3MB, which seems like a reasonable place to
                    # start, i guess...
                    url = '%s/random1500x1500.jpg' % os.path.dirname(
                        best['url'])
                except KeyError:
                    url = None

        if not url:
            cdict = {
                "speed": 0,
                "hosting_provider": 'null',
            }
        else:
            with open('/dev/null', 'wb') as devnull:
                start = time.time()
                req = requests.get(url, stream=True)
                devnull.write(req.content)
                end = time.time()
                total_length = int(req.headers.get('content-length'))
            devnull.close()

            # chop off the float after the 4th decimal point
            # note: not rounding, simply cutting
            # note: dl_time is in seconds
            dl_time = float(end - start)

            if self.units == 'bits' or self.units == 'b':
                unit = 'bps'
                kilo = 1000
                mega = 1000000
                giga = 1000000000
                factor = 8
            elif self.units == 'bytes' or self.units == 'B':
                unit = 'Bps'
                kilo = 8000
                mega = 8000000
                giga = 8000000000
                factor = 1

            if total_length < kilo:
                bps = float(total_length / dl_time)

            if total_length >= kilo and total_length < mega:
                unit = "K" + unit
                bps = float((total_length / 1024.0) / dl_time)

            if total_length >= mega and total_length < giga:
                unit = "M" + unit
                bps = float((total_length / (1024.0 * 1024.0)) / dl_time)

            if total_length >= giga:
                unit = "G" + unit
                bps = float(
                    (total_length / (1024.0 * 1024.0 * 1024.0)) / dl_time)

            bps = "%.2f" % (bps * factor)
            speed = "%s %s" % (bps, unit)
            hosting_provider = '.'.join(urlparse(url).hostname.split('.')[-2:])

            cdict = {
                "speed": speed,
                "hosting_provider": hosting_provider,
            }

        self.output = {
            "full_text": self.format.format(**cdict),
            "color": self.color
        }
コード例 #6
0
    def probe(self):
        self.logger.info("starting %s probe " % (type(self).__name__))

        speedtest.shutdown_event = threading.Event()
        config = speedtest.getConfig()
        closestServer = speedtest.closestServers(config['client'])

        best = None
        serverId = self.probeConfig.serverId
        if serverId == None:
            best = speedtest.getBestServer(closestServer)
        else:
            serverId = "%s" % serverId
            self.logger.debug("use server id [%s]" % (serverId))
            best = speedtest.getBestServer(
                filter(lambda x: x['id'] == serverId, closestServer))
            self.logger.debug("found host for [%s] -> host [%s]" %
                              (serverId, best["host"]))

        transferred, speed, result = None, None, None
        if not "download" in self.probeConfig.direction:
            sizesizes = [int(.25 * 1000 * 1000), int(.5 * 1000 * 1000)]
            sizes = []
            for size in sizesizes:
                for i in range(0, 25):
                    sizes.append(size)
            result = TransferTestResult(units="bit",
                                        transferStart=timezone.now(),
                                        direction="upload",
                                        probeName=self.probeConfig.probeName)
            transferred, speed = self.uploadSpeed(best['url'],
                                                  sizes,
                                                  quiet=True)
            result.transferEnd = timezone.now()
            self.logger.debug('upload: %0.2f M%s/s' %
                              ((speed / 1000 / 1000) * 8, "bit"))

        else:
            sizes = [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000]
            urls = []
            for size in sizes:
                for i in range(0, 4):
                    urls.append('%s/random%sx%s.jpg' %
                                (os.path.dirname(best['url']), size, size))

            result = TransferTestResult(units="bit",
                                        transferStart=timezone.now(),
                                        direction="download",
                                        probeName=self.probeConfig.probeName)
            transferred, speed = self.downloadSpeed(urls, quiet=True)
            result.transferEnd = timezone.now()
            self.logger.debug('download: %0.2f M%s/s' %
                              ((speed / 1000 / 1000) * 8, "bit"))

        result.interfaceIp = getLocalIp("speedtest.net")
        result.transferredUnits = transferred
        result.transferredUnitsPerSecond = speed * 8
        result.host = best["host"]
        result.save()
        self.appendServerInfos(result, best)
        self.logger.info("%s probe done" % (type(self).__name__))
コード例 #7
0
ファイル: net_speed.py プロジェクト: Doix/i3pystatus
    def run(self):

        # since speedtest_cli likes to print crap, we need to squelch it
        @contextlib.contextmanager
        def nostdout():
            save_stdout = sys.stdout
            sys.stdout = StringIO()
            yield
            sys.stdout = save_stdout

        if not self.url:
            with nostdout():
                try:
                    config = speedtest_cli.getConfig()
                    servers = speedtest_cli.closestServers(config['client'])
                    best = speedtest_cli.getBestServer(servers)
                    # 1500x1500 is about 4.3MB, which seems like a reasonable place to
                    # start, i guess...
                    url = '%s/random1500x1500.jpg' % os.path.dirname(best['url'])
                except KeyError:
                    url = None

        if not url:
            cdict = {
                "speed": 0,
                "hosting_provider": 'null',
            }
        else:
            with open('/dev/null', 'wb') as devnull:
                start = time.time()
                req = requests.get(url, stream=True)
                devnull.write(req.content)
                end = time.time()
                total_length = int(req.headers.get('content-length'))
            devnull.close()

            # chop off the float after the 4th decimal point
            # note: not rounding, simply cutting
            # note: dl_time is in seconds
            dl_time = float(end - start)

            if self.units == 'bits' or self.units == 'b':
                unit = 'bps'
                kilo = 1000
                mega = 1000000
                giga = 1000000000
                factor = 8
            elif self.units == 'bytes' or self.units == 'B':
                unit = 'Bps'
                kilo = 8000
                mega = 8000000
                giga = 8000000000
                factor = 1

            if total_length < kilo:
                bps = float(total_length / dl_time)

            if total_length >= kilo and total_length < mega:
                unit = "K" + unit
                bps = float((total_length / 1024.0) / dl_time)

            if total_length >= mega and total_length < giga:
                unit = "M" + unit
                bps = float((total_length / (1024.0 * 1024.0)) / dl_time)

            if total_length >= giga:
                unit = "G" + unit
                bps = float((total_length / (1024.0 * 1024.0 * 1024.0)) / dl_time)

            bps = "%.2f" % (bps * factor)
            speed = "%s %s" % (bps, unit)
            hosting_provider = '.'.join(urlparse(url).hostname.split('.')[-2:])

            cdict = {
                "speed": speed,
                "hosting_provider": hosting_provider,
            }

        self.output = {
            "full_text": self.format.format(**cdict),
            "color": self.color
        }