pcurl 学习 http文件下载及写入空设备文件实例

时间:2023-03-10 02:27:05
pcurl 学习 http文件下载及写入空设备文件实例

import pycurl

import sys

import os

import random

import string

import time

class Transaction(object):

  def __init__(self):

    pass

def run(self):

    c = pycurl.Curl()

    f=open(os.devnull, 'wb')

    c.setopt(c.URL,'http://localhost/GeeHP5M1080P.mp4')

    c.setopt(c.WRITEDATA,f)  #write download content to dev null

    c.setopt(c.HTTPHEADER,["User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)",

        "Accept: */*",                 "Range: bytes=0-",                 "GetContentFeatures.DLNA.ORG: 1",                 "Pragma: getIfoFileURI.dlna.org",                   "Accept-Language: zh-CN",                 "Accept-Encoding: gzip, deflate",                 "DNT: 1",                 "Host: {Host}"])

     #c.setopt(c.BUFFERSIZE,655360)

    c.setopt(c.LOW_SPEED_LIMIT,655360)

    #c.setopt(c.MAX_RECV_SPEED_LARGE, 10485760)

    #c.setopt(c.PROGRESSFUNCTION,self.progress)

    c.setopt(c.FOLLOWLOCATION,1)

    c.setopt(c.MAXREDIRS,5)

    c.setopt(c.VERBOSE,1)

    c.setopt(c.NOPROGRESS, 0)

    c.perform()

      http_code=c.getinfo(c.HTTP_CODE)

      http_conn_time=c.getinfo(c.CONNECT_TIME)

      http_pre_tran=c.getinfo(c.PRETRANSFER_TIME)

      http_start_tran=c.getinfo(c.STARTTRANSFER_TIME)

      http_total_time=c.getinfo(c.TOTAL_TIME)

      http_size=c.getinfo(c.SIZE_DOWNLOAD)

      http_rate=c.getinfo(c.SPEED_DOWNLOAD)

    #print "http_code: %d"%(http_code)

    #print "http_conn_time: %d"%(http_conn_time)

    #print "http_pre_tran: %f"%(http_pre_tran)

      #print "http_start_tran: %f"%(http_start_tran)

    #print "http_total_time: %f"%(http_total_time)

    #print "http_size: %f"%(http_size)

    #print "http_rate: %f"%(http_rate)

if __name__ == '__main__':

  trans = Transaction()

  trans.run()