paramiko
下載一個(gè)文件夾時(shí),便可以使用這個(gè)方法,paramiko模塊提供了ssh及sftp進(jìn)行遠(yuǎn)程登錄服務(wù)器執(zhí)行命令和上傳下載文件的功能。這是一個(gè)第三方的軟件包,使用之前需要先進(jìn)行安裝
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):
"""遠(yuǎn)程下載文件"""
remote_file = sftp_obj.stat(remote_dir_name)
if isdir(remote_file.st_mode):
# 文件夾,不能直接下載,需要繼續(xù)循環(huán)
check_local_dir(local_dir_name)
print('開(kāi)始下載文件夾:' + 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('開(kāi)始下載文件:' + remote_dir_name)
sftp.get(remote_dir_name, local_dir_name)
def check_local_dir(local_dir_name):
"""本地文件夾是否存在,不存在則創(chuàng)建"""
if not os.path.exists(local_dir_name):
os.makedirs(local_dir_name)
if __name__ == "__main__":
"""程序主入口"""
# 服務(wù)器連接信息
host_name = '10.xx.xx.xx'
user_name = 'xx'
password = '***'
port = 22
# 遠(yuǎn)程文件路徑(需要絕對(duì)路徑)
remote_dir = '/opt/xxx090731625.html'
# 本地文件存放路徑(絕對(duì)路徑或者相對(duì)路徑都可以)
local_dir = '/opt/xxxx1625.html'
# 連接遠(yuǎn)程服務(wù)器
t = paramiko.Transport((host_name, port))
t.connect(username=user_name, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
# 遠(yuǎn)程文件開(kāi)始下載
down_from_remote(sftp, remote_dir, local_dir)
# 關(guān)閉連接
t.close()
requests
默認(rèn)會(huì)立即下載文件內(nèi)容并保存到內(nèi)存中,如果文件很大,會(huì)給內(nèi)存造成壓力
import requests
url = 'https://www.python.org/static/img/python-logo@2x.png'
res = requests.get(url)
# 將文件寫(xiě)入pythonimage.png這個(gè)文件中,保存在當(dāng)前程序運(yùn)行的目錄
with open('pythonimage.png', 'wb') as f:
f.write(res.content)
#寫(xiě)入本地磁盤(pán)文件
open('c:/users/LikeGeeks/downloads/PythonImage.png', 'wb').write(res.content)
如果文件很大,會(huì)給內(nèi)存造成壓力,設(shè)置stream參數(shù)為T(mén)rue,這樣,只有當(dāng)我們遍歷iter_content時(shí)才會(huì)進(jìn)行數(shù)據(jù)下載
由于文件內(nèi)容是分塊下載的,因此,可以使用進(jìn)度條來(lái)觀察下載的進(jìn)度(使用clint模塊來(lái)顯示下載進(jìn)度)
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)
# 寫(xiě)入本地磁盤(pán)文件
open('c:/users/LikeGeeks/downloads/PythonImage.png', 'wb').write(res.content)
wget
下載wget庫(kù)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-604139.html
pip install wget
import wget
import ssl
# 取消ssl全局驗(yàn)證
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)
可以縮寫(xiě)成文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-604139.html
f = urllib2.urlopen(url)
with open("pythonimage.png", "wb") as code:
code.write(f.read())
到了這里,關(guān)于python3:四種常見(jiàn)方式從遠(yuǎn)程服務(wù)器下載文件(paramiko、requests、wget、urllib2)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!