栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python使用HTTP在远程文件上查找

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python使用HTTP在远程文件上查找

如果要通过HTTP下载远程文件,则需要设置

Range
标题。

在此示例中检查如何完成。看起来像这样:

myUrlclass.addheader("Range","bytes=%s-" % (existSize))

编辑 :我刚刚发现一个更好的实现。此类很容易使用,因为可以在文档字符串中看到。

class HTTPRangeHandler(urllib2.baseHandler):"""Handler that enables HTTP Range headers.This was extremely simple. The Range header is a HTTP feature tobegin with so all this class does is tell urllib2 that the "206 Partial Content" reponse from the HTTP server is what we expected.Example:    import urllib2    import byterange    range_handler = range.HTTPRangeHandler()    opener = urllib2.build_opener(range_handler)    # install it    urllib2.install_opener(opener)    # create Request and set Range header    req = urllib2.Request('http://www.python.org/')    req.header['Range'] = 'bytes=30-50'    f = urllib2.urlopen(req)"""def http_error_206(self, req, fp, pre, msg, hdrs):    # 206 Partial Content Response    r = urllib.addinfourl(fp, hdrs, req.get_full_url())    r.pre = pre    r.msg = msg    return rdef http_error_416(self, req, fp, pre, msg, hdrs):    # HTTP's Range Not Satisfiable error    raise RangeError('Requested Range Not Satisfiable')

更新
:“更好的实现”已移至github:byterange.py文件中的excid3
/ urlgrabber



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/639473.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号