Using python to implement regular matching to retrieve files from remote FTP directories

  • 2020-04-02 14:42:42
  • OfStack

Encounter a problem, need regular matching the remote FTP directory file, if you use the FTP client can through the command line is easy to do this, but for the time being there is not a tool to support this demand, and through the python support for FTP and support for regular expressions, wrote such a simple tool, used to use regular expressions to match the remote directory file.

The following code


# coding=utf-8
#########################################################################
# File Name: reg_url.py
# Author: WangWeilong
# Company: Baidu
#########################################################################

import re
import sys
import os
from ftplib import FTP

dhccmd = "http://xxx/api/submit"

class DHC_FTP():
 def __init__(self, hostname, username="", passwd=""):
  self.hostname = hostname
  try:
   self.ftp = FTP(self.hostname)
  except:
   print "hostname error!"
   exit(-1)
  self.username = username
  self.passwd = passwd
  self.filelist = []
  self.reg_pattern = ""

 def getftpfilelist(self, path):
  self.ftp.login(self.username, self.passwd)
  self.ftp.cwd(path)
  self.filelist = self.ftp.nlst()

 def matchnames(self, regpattern):
  pattern = re.compile(regpattern)
  matchedfiles = []
  for file in self.filelist:
   match = pattern.search(file)
   if match:
    matchedfiles.append(match.string)
  return matchedfiles



if __name__ == "__main__":
 if len(sys.argv) != 4:
  print 'usage:python reg_url.py $ftpurldir $regular'
 else:
  # parsing ftpurl string 
  head = sys.argv[1].split("@")[1].split("/")[0]
  username = sys.argv[1].split("//")[1].split(":")[0]
  passwd = sys.argv[1].split("//")[1].split(":")[1].split("@")[0]
  pathdir = sys.argv[1].split(head)[1]

  dhc_ftp = DHC_FTP(head, username, passwd)
  dhc_ftp.getftpfilelist("./" + pathdir)

  # Get the regular expression 
  regpattern = r'%s' % sys.argv[2]

  matchedfiles = dhc_ftp.matchnames(regpattern)
  for files in matchedfiles:
   ftpname = sys.argv[1] + "/" + files
   print ftpname

By entering the remote directory address that you want to retrieve and the regular expression that you want to match, you can get to the remote files, and it's a matter of requirements to do with them.

The above is all the content of this article, I hope you can enjoy it.

Please take a moment to share the article with your friends or leave a comment. We sincerely appreciate your support!


Related articles: