zoukankan      html  css  js  c++  java
  • curl/libcurl获取打开网页平均网速

    CURL:

    curl -o /dev/null -s -w %{http_code}:%{http_connect}:%{content_type}:%{time_namelookup}:%{time_redirect}:%{time_pretransfer}:%{time_connect}:%{time_starttransfer}:%{time_total}:%{speed_download} baidu.com

    LIBCURL: 

    size_t WriteCallback(void *ptr, size_t size, size_t nmemb, void *data)
    {
        /* we are not interested in the downloaded bytes itself,
        so we only return the size we would have saved ... */
        (void)ptr;  /* unused */
        (void)data; /* unused */
        return (size_t)(size * nmemb);
    }
    

    //获取下载网速 float GetDownloadSpeed(const string &strURL) { #define CHKSPEED_VERSION "1.0" CURL *curl = curl_easy_init(); float fNetSpeed = -1; if (curl) { curl_easy_setopt(curl, CURLOPT_URL, strURL.c_str()); /* send all data to this function */ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); if (strURL.find("https:") != string::npos) { curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); } /* some servers don't like requests that are made without a user-agent field, so we provide one */ curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-speedchecker/" CHKSPEED_VERSION); /* Perform the request */ CURLcode res = curl_easy_perform(curl); if (!res) { double speed; res = curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &speed); if (!res) { fNetSpeed = speed / 1024.0;//kbyte/sec } } } /* cleanup curl stuff */ curl_easy_cleanup(curl); return fNetSpeed; }

      

    参考资料:

    https://curl.haxx.se/libcurl/c/chkspeed.html

  • 相关阅读:
    【转载】LTE中RB、RE、CP、REG、CCE、子载波
    LTE中,DCI和UCI为什么要定义那么多格式
    LTE中的PDCCH介绍
    ARQ
    (转)MYSQL远程登录权限设置
    (转)忘记wamp-mysql数据库root用户密码重置方法
    phpwind部署问题
    在aliyun遇到一些问题
    (转)PHP5使用cookie时报错 cannot modify header information
    (转)WAMP多站点配置
  • 原文地址:https://www.cnblogs.com/chenyangchun/p/8057287.html
Copyright © 2011-2022 走看看