使用urlgrabber时,建议使用什么方法来处理Content-Encoding: gzip
文件?你知道吗
现在我正在用猴子修补它,就像这样:
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
但它看起来不太干净,我担心它也不安全。。。你知道吗
我想我找到了一个线程安全的解决方案:
但感觉还是不太“干净”:
相关问题 更多 >
编程相关推荐