我想让机器人每小时获取一个 URL,但如果网站运营商是恶意的,他可以让他的服务器向我发送一个 1 GB 的文件。有没有好的方法可以将下载限制为 100 KB,并在该限制之后停止?
我可以想象从头开始编写自己的连接处理程序,但如果可能的话,我想使用 urllib2,只是以某种方式指定限制。
Thanks!
这可能就是您正在寻找的:
import urllib
def download(url, bytes = 1024):
"""Copy the contents of a file from a given URL
to a local file.
"""
webFile = urllib.urlopen(url)
localFile = open(url.split('/')[-1], 'w')
localFile.write(webFile.read(bytes))
webFile.close()
localFile.close()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)