# -*- coding: utf-8 -*-
__author__ = 'gull'
import os, urllib2, log_factory
from urlparse import urlsplit
def get(url, filePath, fileName = None, buffer = 16 * 1024):
log = log_factory.getLogger()
log.info("send http request to %s", url)
def writefile(fsrc, fdst, totalLength):
"""copy data from file-like object fsrc to file-like object fdst"""
if not totalLength:
totalLength = "?"
else:
totalLength = float(totalLength)
bytesRead = 0.0
while 1:
buf = fsrc.read(buffer)
if not buf:
break
fdst.write(buf)
bytesRead += len(buf)
if totalLength != "?":
log.info("%s: %.02f/%.02f kb (%d%%)" % (
fileName,
bytesRead / 1024.0,
totalLength / 1024.0,
100 * bytesRead / totalLength
))
else:
log.info("%s: %.02f/? kb (?%%)" % (
fileName,
bytesRead / 1024.0
))
def getFileName(openUrl):
if 'Content-Disposition' in openUrl.info():
# If the response has Content-Disposition, try to get filename from it
cd = dict(map(
lambda x: x.strip().split('=') if '=' in x else (x.strip(), ''),
openUrl.info().split(';')))
if 'filename' in cd:
filename = cd['filename'].strip("\"'")
if filename: return filename
# if no filename was found above, parse it out of the final URL.
return os.path.basename(urlsplit(openUrl.url)[2])
def getFileLength(openUrl):
return openUrl.info().getheader("Content-Length")
r = urllib2.urlopen(urllib2.Request(url), timeout = 120) #timeout is 120s
try:
fileName = fileName or getFileName(r)
fullfileName = "%s%s%s" % (filePath, os.path.sep, fileName)
totalLength = getFileLength(r)
log.info("write response date to %s", fullfileName)
with open(fullfileName, 'wb') as f:
writefile(r, f, totalLength)
return fileName, totalLength, fullfileName
finally:
r.close()
log.info("http request finished.")
参数说明:
- url:即下载路径,如http://apache.etoak.com/tomcat/tomcat-7/v7.0.20/bin/apache-tomcat-7.0.20.tar.gz
- filePath:下载文件保存的文件夹
- fileName:下载后保存的文件名,可选参数。若为空,则会却reponse header中的filename信息(如下图)继续判空,则取url后缀名(如:apache-tomcat-7.0.20.tar.gz)
可继续加入以下特性:
- 支持代理
- 文件分块,多线程下载
- 异步下载,回调机制
- ...
- 大小: 19.6 KB
分享到:
相关推荐
标题中的"python-devel-2.7.5-68.el7.x86_64.rpm"和"python-2.7.5-68.el7.x86_64.rpm"是两个重要的软件包,它们与Python编程语言在Linux环境中的开发和运行紧密相关。这些文件是针对Red Hat Enterprise Linux 7 ...
python-2.7.5-89.el7.x86_64.rpm python-devel-2.7.5-89.el7.x86_64.rpm python-iniparse-0.4-9.el7.noarch.rpm python-libs-2.7.5-89.el7.x86_64.rpm python-pycurl-7.19.0-19.el7.x86_64.rpm python-urlgrabber-...
1. **编写Python C扩展**:如果你需要编写使用C或C++实现性能关键部分的Python模块,`python-devel` 提供了必要的头文件和库,使得你可以调用Python的API来创建这样的扩展。 2. **编译Python第三方库**:很多Python...
1. **启动安装程序**:运行"python-3.12.1-amd64.exe",用户会被引导到安装向导界面。 2. **选择安装类型**:用户可以选择自定义安装或典型安装。典型安装会自动安装Python及其默认组件,而自定义安装允许用户选择要...
python-3.7.0-amd64.zip python-3.7.0-amd64.zip python-3.7.0-amd64.zip python-3.7.0-amd64.zip python-3.7.0-amd64.zip
《简明 Python 教程》byte-of-python-chinese-edition + pdf
原来是通过命令行下载的docx安装包还没有完全兼容python3,估计这个安装包还只兼容python 2(python2 和python3差别还是挺大的,虽然现在python 3出来很久了,但是不少三方库还没有更新),因此需要自己找一个兼容的包...
ms-python.python-2024.2.1.vsix
python-3.13.0-amd64.exe
python-3.10.0-amd64.exe
python-3.7.0-amd64 安装包
最近ubuntu14.04出了问题,pkg-catkin-pkg无法下载,我7月底重装了一次Ubuntu14.04,安装ros lidigo时报错,依赖python-catkin-pkg,但它不会被安装,我刚开始发现这个问题时试过换源,重装系统均不成功,后面去官方...
python-3.10.4-amd64.exe
Mac OS的Monterey版本可用的python2.7,官方版本,官方地址:https://www.python.org/ftp/python/2.7.18/python-2.7.18-macosx10.9.pkg,转存于CSDN。
python-2.7.5-90.el7.x86_64.rpm
Python-2.7.13-xcompile.patch
python-3.8.0-amd64.exe, python-windows平台,使用64位,exe安装包
python-3.10.6-macos11.pkg
资源分类:Python库 所属语言:Python 资源全名:python-docx-0.8.9.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
python-urlgrabber-3.9.1-11.el6.noarch.rpm