您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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 to
begin 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, code, msg, hdrs):
    # 206 Partial Content Response
    r = urllib.addinfourl(fp, hdrs, req.get_full_url())
    r.code = code
    r.msg = msg
    return r

def http_error_416(self, req, fp, code, msg, hdrs):
    # HTTP's Range Not Satisfiable error
    raise RangeError('Requested Range Not Satisfiable')

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

python 2022/1/1 18:26:30 有583人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶