import pycurl
from io import BytesIO

class ex_response(object):
    def __init__(self,url):
        self.buffer = BytesIO()
        self.c = pycurl.Curl()
        self.c.setopt(pycurl.URL,url)
        self.c.setopt(pycurl.WRITEDATA, self.buffer)
        self.c.setopt(pycurl.WRITEHEADER,self.buffer)
        try:
            self.c.perform()
        except Exception as e:
            print('connection error:' + str(e))
            self.buffer.close()
            self.c.close()

    def getinfo(self):

        http_code = self.c.getinfo(pycurl.HTTP_CODE)  # 状态码
        total_time = self.c.getinfo(pycurl.TOTAL_TIME)  # 传输结束总消耗时间
        dns_time = self.c.getinfo(pycurl.NAMELOOKUP_TIME)  # DNS解析时间
        connect_time = self.c.getinfo(pycurl.CONNECT_TIME)  # 建立连接时间
        pretransfer_time = self.c.getinfo(pycurl.PRETRANSFER_TIME)  # 建立连接到准备传输消耗时间
        first_byte_time = self.c.getinfo(pycurl.STARTTRANSFER_TIME)  # 从建立连接到传输开始消耗时间
        info = {"http_code": http_code,
                "total_time": total_time,
                "dns_time": dns_time,
                "connect_time": connect_time,
                "pretransfer_time": pretransfer_time,
                "first_byte_time":first_byte_time
                }
        return  info
        self.buffer.close()
        self.c.close()

if __name__ == '__main__':
    curl_respon = ex_response("https://longvideoh5.piaoquantv.com/core/share?shareSource=customerMessage&fromAppType=0&qrAppType=0&versionCode=321&shareUid=12463024&shareMachineCode=weixin_openid_o0w175fPwp8yrtOGihYJhvnT9Ag4&h5WxrootPageSource=vlog-pages___category&videoId=2689415&isRecommendShare=1&h5ShareId=backend493cd67dd28f4ee395781d59881567211625976055926&shareDepth=0&state=#")
    http_code, total_time, dns_time, connect_time, pretransfer_time, first_byte_time = curl_respon.getinfo()
    print(http_code, total_time*1000, dns_time*1000, connect_time*1000, pretransfer_time*1000, first_byte_time*1000)