Content-Encoding: gzip
使用urlgrabber时处理文件的推荐方法是什么?
现在我正在像这样修补它:
g = URLGrabber(http_headers=(("Accept-Encoding", "gzip"),))
g.is_compressed = False # I don't know yet if the server will send me compressed data
# Backup current method of handling downloaded headers
try:
PyCurlFileObject.orig_hdr_retrieve
except AttributeError:
PyCurlFileObject.orig_hdr_retrieve = PyCurlFileObject._hdr_retrieve
def hdr_retrieve(instance, buf):
r = PyCurlFileObject.orig_hdr_retrieve(instance, buf)
if "content-encoding" in buf.lower() and "zip" in buf.lower():
g.is_compressed = True
return r
PyCurlFileObject._hdr_retrieve = hdr_retrieve
g.urlgrab(url, dest)
if g.is_compressed:
# ungzip file here
但它看起来不是很干净,我担心它也不是线程安全的......