python 实现从服务器下载文件
创始人
2025-01-18 08:02:55
0

python 实现从服务器下载文件

文章目录

  • python 实现从服务器下载文件
    • 1、使用python paramiko库
    • 2、使用Python wget库
    • 3、使用Python urllib库
    • 4、使用subprocess.run()执行scp命令
    • 5、使用os.system() 执行scp命令

通过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')  

相关内容

热门资讯

避坑细节!微信微乐小程序有挂黑... 避坑细节!微信微乐小程序有挂黑科技,微信小程序微乐辅助器ios-确实有辅助工具(确实有挂)在进入微信...
推荐几款新版!AAPoKer辅... 推荐几款新版!AAPoKer辅助挂多功能透视工具,果然是有挂(有挂秘籍)1、金币登录送、破产送、升级...
一分钟了解!德扑之星辅助挂多功... 一分钟了解!德扑之星辅助挂多功能透视工具,其实真的是有挂(有挂讲解)德扑之星能透视中分为三种模型:德...
今日焦点!微乐小程序微乐辅助软... 今日焦点!微乐小程序微乐辅助软件,微乐麻将脚本透视-果然有辅助神器(新版有挂)该软件可以轻松地帮助玩...
一秒答解!WPK辅助挂多功能透... 一秒答解!WPK辅助挂多功能透视工具,都是存在有挂(有人有挂)1、一秒答解!WPK辅助挂多功能透视工...
详细说明!微信小程序微乐房间怎... 详细说明!微信小程序微乐房间怎么开挂,微信小程序游戏辅助-本来真的有辅助app(真的有挂)1、进入到...
玩家必备科普!aapoker辅... 玩家必备科普!aapoker辅助挂多功能透视工具,真是真的是有挂(有挂细节)aapoker破解侠是真...
推荐十款!微信微乐辅助免费,微... 推荐十款!微信微乐辅助免费,微乐自建房辅助软件-切实是有辅助脚本(有挂攻略)1、任何微信微乐辅助免费...
实测揭晓!微信小程序微乐辅助免... 您好,微信小程序微乐辅助免费这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】...
我来分享!AAPoKer辅助挂... 我来分享!AAPoKer辅助挂多功能透视工具,确实真的是有挂(果真有挂)1、让任何用户在无需AAPo...