from ftplib import FTP_TLS import os import re class MyFtp(FTP_TLS): """继承FTP类""" def dirs(self, *args): """my dirs""" self.encoding = 'GB18030' cmd = 'LIST' if args[-1:] and not isinstance(args[-1], str): args, func = args[:-1], args[-1] for arg in args: if arg: cmd = cmd + (' ' + arg) files = [] self.retrlines(cmd, files.append) l_files = [file.split(" ")[-1] for file in files] return [file for file in l_files if file != "." and file != ".."] ftps = MyFtp() # ftps.set_debuglevel(2) ftps.connect("172.25.22.1",21) #FTP服务IP ftps.login("user","123456") #FTP用户密码 ftps.prot_p() #继承了MyFtp类中的prot_p方法,目的是为了解决某些开启了安全SSL/TSL而连接不上的问题 ftps.set_pasv(1) #pasv(1)为被动模式,pasv(0为主动模式) ftps.encoding = 'GB18030' #防止中文乱码 ftps.retrlines('LIST') # a = ftps.dirs("/") # print(a) ftps.close()