Home > Backend Development > Python Tutorial > python method to download the entire ftp directory

python method to download the entire ftp directory

高洛峰
Release: 2017-02-21 10:27:07
Original
2401 people have browsed it

This article mainly introduces the method of downloading the entire ftp directory in Python. The article gives detailed sample codes. I believe it has certain reference value for everyone's understanding and learning. Friends in need can learn together. .

Preface

I recently wrote this script due to business needs. The task completed by the script is to download a directory from FTP. Everyone knows to download a directory from FTP. You can use the get command to download multiple files. You can use mget to download multiple files, but you have to download a directory. Sorry, you can't. If you have to compete, you said to use lftp, and then use the mirror command. I am not serious about this, because every time Each company has its own scenario, just choose the appropriate one. Because our FTP all uses SSL, so lftp cannot be used. The basic idea of ​​the script is to establish an FTP connection, then log in to get the file list, and based on the returned list Perform a for loop and download one by one.

The script is as follows:

#!/usr/bin/evn python
 
from ftplib import FTP_TLS, FTP
import socket
import ssl
import os
import sys
 
class IMPLICIT_FTP_TLS(FTP_TLS):
 #构造函数初始化父类
 def __init__(self, host='', user='', passwd='', acct='', keyfile=None,
  certfile=None, timeout=60):
  FTP_TLS.__init__(self, host, user, passwd, acct, keyfile, certfile, timeout)
 #建立服务器的FTPS连接
 def connect(self, host='', port=0, timeout=-999):
  if host != '':
   self.host = host
  if port > 0:
   self.port = port
  if timeout != -999:
   self.timeout = timeout
  try:
   #创建socket
   self.sock = socket.create_connection((self.host, self.port), self.timeout)
   self.af = self.sock.family
   #wrap_socket接收一个socket实例,返回SSLSocket实例,可以理解在普通socket上封装了一层ssl
   self.sock = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
   self.file = self.sock.makefile('rb')
   self.welcome = self.getresp()
  except Exception as e:
   print (e)
  return self.welcome
 
 
def get_ftp_ver(version):
 #如果没有这个目录就新建
 if not os.path.isdir(version):
  os.makedirs(version)
 ftps = IMPLICIT_FTP_TLS()
 ftps.connect(host='10.0.0.8', port=666)
 ftps.login(user="ftp_user", passwd="ftp_password")
 #建立安全的数据连接,之后才能返回数据。
 ftps.prot_p()
 ftps.cwd(version)
 files = ftps.nlst()
 #进入本地目录
 os.chdir(version)
 #循环下载每个文件
 for file in files:
  fp = open(file, 'wb')
  ftps.retrbinary('RETR %s' % file, fp.write)
 ftps.close()
 
if __name__ == '__main__':
 get_ftp_ver(sys.argv[1])
Copy after login

##Script usage:

#python get_data.py version_20160920
Copy after login

The parameters following are basically the file names provided by R&D for you to update. Then you can download the files in the entire directory by running it. You will understand the other contents of the script by reading the comments.

For more python methods to download the entire ftp directory and related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template