In this article, the example for you to share the python implementation of FTP cycle upload file specific code, for your reference, the details are as follows
During testing, sometimes you will use the FTP data stream, or you need to use FTP to upload files repeatedly, so you write a python code for FTP cyclic file upload.
The code is as follows:
#coding=utf-8 import sys import os from ftplib import FTP from time import sleep _XFER_FILE = 'FILE' _XFER_DIR = 'DIR' class Transmitter(object): # Note: recursively upload local files or dirs to ftp server def __init__(self): = None def __del__(self): pass def setFtpParams(self, ip, uname, pwd, port=21, timeout=60): = ip = uname = pwd = port = timeout def initEnv(self): if is None: = FTP() print('### Connecting to an FTP server: %s ...'' % ) (, , ) (, ) def clearEnv(self): if : () print('### Disconnect from FTP server: %s!' % ) = None def uploadDir(self, localdir='./', remotedir='./'): if not (localdir): return (remotedir) for file in (localdir): src = (localdir, file) if (src): (src, file) elif (src): try: (file) except: ('Directory exists %s' % file) (src, file) ('..') def uploadFile(self, localpath, remotepath='./'): if not (localpath): return print('+++ upload %s to %s:%s' % (localpath, , remotepath)) ('STOR ' + remotepath, open(localpath, 'rb')) sleep(0.5) try: (remotepath) except: pass # del file when uploaded this file # (localpath) # sleep(1) def __filetype(self, src): if (src): index = ('\\') if index == -1: index = ('/') return _XFER_FILE, src[index + 1:] elif (src): return _XFER_DIR, '' def upload(self, src): filetype, filename = self.__filetype(src) () if filetype == _XFER_DIR: = src () elif filetype == _XFER_FILE: (src, filename) () if __name__ == '__main__': srcDir = r'C:\Users\Administrator\Downloads\FTP\smp' transmitter = Transmitter() ('10.44.0.2', 'admin', '123123') while True: (srcDir) sleep(4)
This is the whole content of this article.