python 快速获取图片大小

实现原理

利用header请求原则,只进行header请求获取图片info,有效缩短时间,减少图片下载解析的过程:

                    url = "http://avatar.csdn.net/0/A/C/1_hi_chen_xingwang.jpg"
                    rq = urllib2.Request(url,)
                    rq.get_method = lambda:'HEAD'
                    rp = urllib2.urlopen(rq,timeout= 10)
                    msgs = rp.info().headers
                    msg = rp.info()
                    size = 0
                    # size = msgs.items()[0][1]
                    for i in xrange(0,len(msgs)):
                        pass
                        head = msgs[i][:len('Content-Length')]
                        # print head
                        if str(head) == "Content-Length":
                            pass
                            size = msgs[i].split(':')[1]
                            # print size
                        else:
                            pass
                            print '------------------------------'
                    print "--------->size<---------"
                    image_size = int(size)/1024

                    print image_size

你可能感兴趣的:(Python,Sugar)