其他分享
首页 > 其他分享> > Pycurl获取http响应过程时间

Pycurl获取http响应过程时间

作者:互联网

安装

pip install pycurl

示例代码

import pycurl

class Test:
    def __init__(self):
        self.contents = ''

    def body_callback(self, buf):
        self.contents = self.contents + buf

def gzip(url):
    t = Test()
    c = pycurl.Curl()
    # c.setopt(pycurl.WRITEFUNCTION, t.body_callback)
    # c.setopt(pycurl.ENCODING,'gzip')
    c.setopt(pycurl.URL, url)
    c.perform()

    print('状态码', c.getinfo(pycurl.HTTP_CODE))
    print('域名解析时间', c.getinfo(pycurl.NAMELOOKUP_TIME))
    print('远程服务器连接时间', c.getinfo(pycurl.CONNECT_TIME))
    print('连接上后到开始传输时的时间', c.getinfo(pycurl.PRETRANSFER_TIME))
    print('接收到第一个字节的时间', c.getinfo(pycurl.STARTTRANSFER_TIME))
    print('上一请求总的时间', c.getinfo(pycurl.TOTAL_TIME))
    print('如果存在转向的话,花费的时间', c.getinfo(pycurl.REDIRECT_TIME))
if __name__ == '__main__':
    gzip('https://github.com/hanzhichao')

pycurl 的一些响应信息

<参考: http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html>

标签:__,http,Pycurl,getinfo,响应,TIME,print,HTTP,pycurl
来源: https://www.cnblogs.com/superhin/p/16340055.html