使用pycurl获取http请求的content-length

来源:互联网 发布:总结网络推广的工具 编辑:程序博客网 时间:2024/06/05 20:05
import pycurlfrom sStringIO import StringIOclass _PyCurlDownload():    '''先请求一次,得到要下载的大小Content-Length,再进行download'''    def __init__(self, url):        self._header = StringIO()        self._curl = pycurl.Curl()        self._curl.setopt(pycurl.URL, url)        self._curl.setopt(pycurl.HEADERFUNCTION, self._header.write)        self._hasHeadr = False             def getContentLength(self):        if not self._hasHeadr:            self._curl.setopt(pycurl.NOBODY, 1)            self._curl.perform()            self._hasHeadr = True        return self._curl.getinfo(pycurl.CONTENT_LENGTH_DOWNLOAD)        def download(self, writeFunction, progress = lambda a,b,c,d:None):        '''block function ,return: HTTP CODE'''        c = self._curl        c.setopt(pycurl.NOBODY, 0)        c.setopt(c.WRITEFUNCTION, writeFunction)                c.setopt(c.NOPROGRESS, 0)        c.setopt(c.PROGRESSFUNCTION, progress)        c.setopt(c.FOLLOWLOCATION, 1)        c.setopt(c.MAXREDIRS, 5)            c.setopt(pycurl.SSL_VERIFYHOST, 1)        c.setopt(pycurl.SSL_VERIFYPEER, 0)        try:            c.perform()        except error, e:            c.close()            raise AbortException, e        code = int(c.getinfo(c.HTTP_CODE))        c.close()        return code    if __name__ == "__main__":    c = _PyCurlDownload('http://www.baidu.com')    cl = c.getContentLength()    s = StringIO()    c.download(s.write)    assert len(s.getvalue()) == cl    print len(s.getvalue()) , cl

原创粉丝点击