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])
##Script usage:
#python get_data.py version_20160920The 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!

This article explains how to use Beautiful Soup, a Python library, to parse HTML. It details common methods like find(), find_all(), select(), and get_text() for data extraction, handling of diverse HTML structures and errors, and alternatives (Sel

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

Serialization and deserialization of Python objects are key aspects of any non-trivial program. If you save something to a Python file, you do object serialization and deserialization if you read the configuration file, or if you respond to an HTTP request. In a sense, serialization and deserialization are the most boring things in the world. Who cares about all these formats and protocols? You want to persist or stream some Python objects and retrieve them in full at a later time. This is a great way to see the world on a conceptual level. However, on a practical level, the serialization scheme, format or protocol you choose may determine the speed, security, freedom of maintenance status, and other aspects of the program

Python's statistics module provides powerful data statistical analysis capabilities to help us quickly understand the overall characteristics of data, such as biostatistics and business analysis. Instead of looking at data points one by one, just look at statistics such as mean or variance to discover trends and features in the original data that may be ignored, and compare large datasets more easily and effectively. This tutorial will explain how to calculate the mean and measure the degree of dispersion of the dataset. Unless otherwise stated, all functions in this module support the calculation of the mean() function instead of simply summing the average. Floating point numbers can also be used. import random import statistics from fracti

This article compares TensorFlow and PyTorch for deep learning. It details the steps involved: data preparation, model building, training, evaluation, and deployment. Key differences between the frameworks, particularly regarding computational grap

This tutorial builds upon the previous introduction to Beautiful Soup, focusing on DOM manipulation beyond simple tree navigation. We'll explore efficient search methods and techniques for modifying HTML structure. One common DOM search method is ex

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

This article guides Python developers on building command-line interfaces (CLIs). It details using libraries like typer, click, and argparse, emphasizing input/output handling, and promoting user-friendly design patterns for improved CLI usability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)