python 实现从服务器下载文件
文章目录
通过python编程,实现从服务器下载文件方法有很多,这里列举常用的几种方法:
-
使用python paramiko库
-
使用python wget库
-
使用python urllib库
-
使用subprocess.run()执行scp命令
-
使用os.system() 执行scp命令
1、使用python paramiko库
import inspect
import paramiko
remote_ip = "192.168.3.211"
remote_port = 22
remote_username = "root"
remote_password = "123456"
print(f"Line {inspect.currentframe().f_lineno}: username: {remote_username}, password: {remote_password}")
# 创建SSHClient对象
ssh = paramiko.SSHClient()
# 允许连接不在known_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接到远程服务器
ssh.connect(hostname=remote_ip, username=remote_username, password=remote_password)
sftp = ssh.open_sftp()
filename = "main.txt"
print(f'Line {inspect.currentframe().f_lineno}: 正在下载文件...')
sftp.get("/home/main.txt", filename)
sftp.close()
ssh.close()
print(f"Line {inspect.currentframe().f_lineno}: 下载完成")
2、使用Python wget库
Python的wget库是一个用于下载文件的库,支持从FTP服务器上下载文件。使用Python wget库下载文件的步骤如下:
1.安装wget库
pip install wget
2.下载文件
import wget
url = "ftp://username:password@ftp.example.com/data/example.txt"
filename = wget.download(url)
3、使用Python urllib库
Python的urllib库可以用于下载文件,包括从FTP服务器上下载文件。使用Python urllib库下载文件的步骤如下:
1.导入urllib库
import urllib.request
2.创建FTP URL
url = "ftp://username:password@ftp.example.com/data/example.txt"
3.下载文件
filename = "example.txt"
urllib.request.urlretrieve(url, filename)
4、使用subprocess.run()执行scp命令
import os
import sys
import inspect
import paramiko
import subprocess
remote_ip = "192.168.11.234"
remote_port = 22
remote_username = "root"
remote_password = "123456"
# 创建SSHClient对象
ssh = paramiko.SSHClient()
# 允许连接不在known_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接到远程服务器
ssh.connect(hostname=remote_ip, username=remote_username, password=remote_password)
# 输入本地文件的路径,并且判断是否存在
while True:
local_folder = input('请输入保存到本地的路径: ./\n')
# 判断文件是否存在
if os.path.exists(local_folder):
break # 如果文件存在,则跳出循环
else:
print('\033[31m' +
f'文件 {local_folder} 不存在,请重新输入。' + '\033[0m')
local_folder = r"{}".format(local_folder)
remote_folder = input('请输入远程服务器端下载的文件的路径: '
'home/main.txt \n')
try:
print('正在下载文件...')
print(f"Line {inspect.currentframe().f_lineno}: username : {ssh.get_transport().get_username()}")
print(f"Line {inspect.currentframe().f_lineno}: host_ip : {ssh.get_transport().getpeername()[0]}")
print(f"Line {inspect.currentframe().f_lineno}: host_port : {ssh.get_transport().getpeername()[1]}")
# 要传输的文件路径
local_file_path = local_folder
remote_file_path = remote_folder
# 执行scp命令
result = subprocess.run(['scp', f'{ssh.get_transport().get_username()}@{ssh.get_transport().getpeername()[0]}:{remote_file_path}', local_file_path])
# result = subprocess.run(['scp', local_file_path, f'{ssh.get_transport().get_username()}@{ssh.get_transport().getpeername()[0]}:{remote_file_path}'])
print(f"Line {inspect.currentframe().f_lineno}: 输出结果为, result : ", result)
# print(f"Line {inspect.currentframe().f_lineno}: 输出结果为, result.decode() :", result.decode())
except Exception as e:
print('func:{}(),line:{},'.format(sys._getframe().f_code.co_name, sys._getframe().f_lineno), end="")
print('\033[31m' + f'文件下载失败,报错信息为: {e}' + '\033[0m')
5、使用os.system() 执行scp命令
import os
import sys
import inspect
import paramiko
import subprocess
remote_ip = "192.168.3.234"
remote_port = 22
remote_username = "root"
remote_password = "123456"
# 创建SSHClient对象
ssh = paramiko.SSHClient()
# 允许连接不在known_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接到远程服务器
ssh.connect(hostname=remote_ip, username=remote_username, password=remote_password)
# 输入本地文件的路径,并且判断是否存在
while True:
local_folder = input('请输入保存到本地的路径: ./\n')
# 判断文件是否存在
if os.path.exists(local_folder):
break # 如果文件存在,则跳出循环
else:
print('\033[31m' +
f'文件 {local_folder} 不存在,请重新输入。' + '\033[0m')
local_folder = r"{}".format(local_folder)
remote_folder = input('请输入远程服务器端下载的文件的路径: '
'/home/main.txt \n')
try:
print('正在下载文件...')
print(f"Line {inspect.currentframe().f_lineno}: username : {ssh.get_transport().get_username()}")
print(f"Line {inspect.currentframe().f_lineno}: host_ip : {ssh.get_transport().getpeername()[0]}")
print(f"Line {inspect.currentframe().f_lineno}: host_port : {ssh.get_transport().getpeername()[1]}")
# 要传输的文件路径
local_file_path = local_folder
remote_file_path = remote_folder
# 执行scp命令
result = subprocess.run(['scp', f'{ssh.get_transport().get_username()}@{ssh.get_transport().getpeername()[0]}:{remote_file_path}', local_file_path])
# result = subprocess.run(['scp', local_file_path, f'{ssh.get_transport().get_username()}@{ssh.get_transport().getpeername()[0]}:{remote_file_path}'])
print(f"Line {inspect.currentframe().f_lineno}: 输出结果为, result : ", result)
# print(f"Line {inspect.currentframe().f_lineno}: 输出结果为, result.decode() :", result.decode())
except Exception as e:
print('func:{}(),line:{},'.format(sys._getframe().f_code.co_name, sys._getframe().f_lineno), end="")
print('\033[31m' + f'文件下载失败,报错信息为: {e}' + '\033[0m')