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

相关内容

热门资讯

攻略辅助!新九天作弊系统(辅助... 攻略辅助!新九天作弊系统(辅助)原来一直总是有辅助攻略(哔哩哔哩)进入游戏-大厅左侧-新手福利-激活...
黑科技辅助挂!超级三加一辅助工... 黑科技辅助挂!超级三加一辅助工具!总是是有开挂辅助教程(今日头条)-哔哩哔哩黑科技辅助挂!超级三加一...
绝活辅助!新蜜瓜大厅破解(辅助... 绝活辅助!新蜜瓜大厅破解(辅助)确实是有辅助软件(哔哩哔哩)暗藏猫腻,小编详细说明新蜜瓜大厅破解破解...
黑科技插件!丽水都莱辅助工具试... 您好,丽水都莱辅助工具试用这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...
据权威媒体报道!pokemmo... 据权威媒体报道!pokemmo辅助器手机版下载!确实确实有开挂辅助教程(详细教程)-哔哩哔哩1.po...
攻略辅助!逍遥辅助下载地址(辅... 攻略辅助!逍遥辅助下载地址(辅助)原来是真的有辅助方法(哔哩哔哩)1、下载好逍遥辅助下载地址正确养号...
2026版教程!海盗来了辅助哪... 2026版教程!海盗来了辅助哪个好!都是是有开挂辅助挂(有挂技术)-哔哩哔哩1、海盗来了辅助哪个好免...
攻略辅助!情怀游戏辅助器(辅助... 攻略辅助!情怀游戏辅助器(辅助)一贯真的有辅助插件(哔哩哔哩)1、实时情怀游戏辅助器透视辅助更新:用...
演示辅助!蘑菇云辅助(辅助)果... 演示辅助!蘑菇云辅助(辅助)果然存在有辅助攻略(哔哩哔哩)1、该软件可以轻松地帮助玩家将蘑菇云辅助辅...
今天下午!福建大玩家辅助器!总... 今天下午!福建大玩家辅助器!总是存在有开挂辅助神器(有挂秘籍)-哔哩哔哩1、今天下午!福建大玩家辅助...