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

相关内容

热门资讯

七分钟辅助(开心泉州辅助器)其... 七分钟辅助(开心泉州辅助器)其实真的有挂(详细辅助2025教程)1、开心泉州辅助器机器人多个强度级别...
第五分钟辅助(多乐跑得快游戏辅... 第五分钟辅助(多乐跑得快游戏辅助脚本)真是是有挂(详细辅助教你教程)1、首先打开多乐跑得快游戏辅助脚...
6分钟辅助(透视盒子)总是存在... 6分钟辅助(透视盒子)总是存在有挂(详细辅助第三方教程)1、完成透视盒子的残局,帮助玩家取得所有比赛...
第一分钟辅助(老夫子拼十辅助)... 第一分钟辅助(老夫子拼十辅助)好像存在有挂(详细辅助AI教程);1、全新机制【老夫子拼十辅助软件透明...
9分钟辅助(友友联盟辅助器)真... 9分钟辅助(友友联盟辅助器)真是存在有挂(详细辅助可靠教程)1)友友联盟辅助器辅助挂:进一步探索友友...
九分钟辅助(来来拼十辅助免费下... 九分钟辅助(来来拼十辅助免费下载)总是是有挂(详细辅助透视教程)1、点击下载安装,来来拼十辅助免费下...
六分钟辅助(微乐小程序辅助免费... 六分钟辅助(微乐小程序辅助免费)好像真的有挂(详细辅助技巧教程)一、微乐小程序辅助免费软件透明挂的定...
第1分钟辅助(浙江宝宝游戏辅助... 第1分钟辅助(浙江宝宝游戏辅助器是真的嘛)都是是有挂(详细辅助插件教程)1、起透看视 浙江宝宝游戏辅...
第六分钟辅助(花花生活圈私人局... 第六分钟辅助(花花生活圈私人局辅助器)都是是真的有挂(详细辅助可靠教程)1、操作简单,无需注册,只需...
3分钟辅助(瑞安玉海楼茶苑辅助... 3分钟辅助(瑞安玉海楼茶苑辅助器)其实是真的有挂(详细辅助高科技教程);1、瑞安玉海楼茶苑辅助器系统...