ex_response.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import pycurl
  2. from io import BytesIO
  3. class ex_response(object):
  4. def __init__(self,url):
  5. self.buffer = BytesIO()
  6. self.c = pycurl.Curl()
  7. self.c.setopt(pycurl.URL,url)
  8. self.c.setopt(pycurl.WRITEDATA, self.buffer)
  9. self.c.setopt(pycurl.WRITEHEADER,self.buffer)
  10. try:
  11. self.c.perform()
  12. except Exception as e:
  13. print('connection error:' + str(e))
  14. self.buffer.close()
  15. self.c.close()
  16. def getinfo(self):
  17. http_code = self.c.getinfo(pycurl.HTTP_CODE) # 状态码
  18. total_time = self.c.getinfo(pycurl.TOTAL_TIME) # 传输结束总消耗时间
  19. dns_time = self.c.getinfo(pycurl.NAMELOOKUP_TIME) # DNS解析时间
  20. connect_time = self.c.getinfo(pycurl.CONNECT_TIME) # 建立连接时间
  21. pretransfer_time = self.c.getinfo(pycurl.PRETRANSFER_TIME) # 建立连接到准备传输消耗时间
  22. first_byte_time = self.c.getinfo(pycurl.STARTTRANSFER_TIME) # 从建立连接到传输开始消耗时间
  23. info = {"http_code": http_code,
  24. "total_time": total_time,
  25. "dns_time": dns_time,
  26. "connect_time": connect_time,
  27. "pretransfer_time": pretransfer_time,
  28. "first_byte_time":first_byte_time
  29. }
  30. return info
  31. self.buffer.close()
  32. self.c.close()
  33. if __name__ == '__main__':
  34. 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=#")
  35. http_code, total_time, dns_time, connect_time, pretransfer_time, first_byte_time = curl_respon.getinfo()
  36. print(http_code, total_time*1000, dns_time*1000, connect_time*1000, pretransfer_time*1000, first_byte_time*1000)