paramiko
下载一个文件夹时,便可以使用这个方法,paramiko模块提供了ssh及sftp进行远程登录服务器执行命令和上传下载文件的功能。这是一个第三方的软件包,使用之前需要先进行安装
pip install paramiko
import paramiko
import os
from stat import S_ISDIR as isdir
def down_from_remote(sftp_obj, remote_dir_name, local_dir_name):
"""远程下载文件"""
remote_file = sftp_obj.stat(remote_dir_name)
if isdir(remote_file.st_mode):
# 文件夹,不能直接下载,需要继续循环
check_local_dir(local_dir_name)
print('开始下载文件夹:' + remote_dir_name)
for remote_file_name in sftp.listdir(remote_dir_name):
sub_remote = os.path.join(remote_dir_name, remote_file_name)
sub_remote = sub_remote.replace('\\', '/')
sub_local = os.path.join(local_dir_name, remote_file_name)
sub_local = sub_local.replace('\\', '/')
down_from_remote(sftp_obj, sub_remote, sub_local)
else:
# 文件,直接下载
print('开始下载文件:' + remote_dir_name)
sftp.get(remote_dir_name, local_dir_name)
def check_local_dir(local_dir_name):
"""本地文件夹是否存在,不存在则创建"""
if not os.path.exists(local_dir_name):
os.makedirs(local_dir_name)
if __name__ == "__main__":
"""程序主入口"""
# 服务器连接信息
host_name = '10.xx.xx.xx'
user_name = 'xx'
password = '***'
port = 22
# 远程文件路径(需要绝对路径)
remote_dir = '/opt/xxx090731625.html'
# 本地文件存放路径(绝对路径或者相对路径都可以)
local_dir = '/opt/xxxx1625.html'
# 连接远程服务器
t = paramiko.Transport((host_name, port))
t.connect(username=user_name, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
# 远程文件开始下载
down_from_remote(sftp, remote_dir, local_dir)
# 关闭连接
t.close()
requests
默认会立即下载文件内容并保存到内存中,如果文件很大,会给内存造成压力
import requests
url = 'https://www.python.org/static/img/python-logo@2x.png'
res = requests.get(url)
# 将文件写入pythonimage.png这个文件中,保存在当前程序运行的目录
with open('pythonimage.png', 'wb') as f:
f.write(res.content)
#写入本地磁盘文件
open('c:/users/LikeGeeks/downloads/PythonImage.png', 'wb').write(res.content)
如果文件很大,会给内存造成压力,设置stream参数为True,这样,只有当我们遍历iter_content时才会进行数据下载
由于文件内容是分块下载的,因此,可以使用进度条来观察下载的进度(使用clint模块来显示下载进度)
import requests
from clint.textui import progress
url = 'https://www.python.org/ftp/python/3.8.1/python-3.8.1-macosx10.9.pkg'
res = requests.get(url, stream=True)
total_length = int(res.headers.get('content-length'))
with open("py.pkg", "wb") as pypkg:
for chunk in progress.bar(res.iter_content(chunk_size=1024), expected_size=(total_length/1024) + 1, width=100):
if chunk:
pypkg.write(chunk)
# 写入本地磁盘文件
open('c:/users/LikeGeeks/downloads/PythonImage.png', 'wb').write(res.content)
wget
下载wget库文章来源:https://www.toymoban.com/news/detail-604139.html
pip install wget
import wget
import ssl
# 取消ssl全局验证
ssl._create_default_https_context = ssl._create_unverified_context
url = 'https://www.python.org/static/img/python-logo@2x.png'
wget.download(url, 'pythonlogo.png')
urllib2
import urllib2
url = 'https://www.python.org/static/img/python-logo@2x.png'
f = urllib2.urlopen(url)
data = f.read()
with open("pythonimage.png", "wb") as code:
code.write(data)
可以缩写成文章来源地址https://www.toymoban.com/news/detail-604139.html
f = urllib2.urlopen(url)
with open("pythonimage.png", "wb") as code:
code.write(f.read())
到了这里,关于python3:四种常见方式从远程服务器下载文件(paramiko、requests、wget、urllib2)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!