python从网络下载文件并获得文件大小、文件类型
import urllib2 from settings import COOKIES opener = urllib2.build_opener() cookies = ";".join("%s=%s" % (k, v) for k, v in COOKIES.items()) opener.addheaders.append(('Cookie', cookies)) req = opener.open(link) meta = req.info() file_size = int(meta.getheaders("Content-Length")[0]) content_type = meta.getheaders('Content-Type')[0].split(';')[0] print file_size, content_type open(file_name, 'wb').write(req.read())