Back

Python 获取下载文件的大小

# -*- coding: utf-8 -*-

from urlparse import urlparse

url = "http://im.baidu.com/download/BaiduHi_2.4_Beta.exe" parsedurl = urlparse(url) net_loc = parsedurl[1] path = parsedurl[2] httpConn = httplib.HTTPConnection(net_loc); httpConn.request("GET", path) r = httpConn.getresponse() if r.status == 200: size = r.getheader('Content-Length') size = int(size) / 1024 print 'size:', size print r.getheader('Content-Type') #类型
print r.getheader('Last-Modified') #修改时间
else: print r.status, r.reason

httpConn.close()