Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ftp script issues
#1
I'm getting stuck with my ftp.. tried to include an FTP script in with mine that uploads all files in the folder, but fails after the first one with an error 550.

if I run the ftp script on it's on it works like a charm, when I include it in my watchdog script it fails. I've even tried to include a bash script I've used before and it doesn't like that either, so ive reverted to the upload everything in the folder but it fails after the first one is transfered.

import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from subprocess import call
import os
from time import sleep
import datetime
from ftplib import FTP, error_perm

class Watcher:
    def __init__(self):
        self.dir = os.path.abspath('/home/pi/Videos/new_mp4/')
        self.observer = Observer()
 
    def run(self):
        event_handler = Handler()
        self.observer.schedule(event_handler, self.dir, recursive=True)
        self.observer.start()
        try:
            while True:
                time.sleep(5)
        except:
            self.observer.stop()
            print("Error")
 
        self.observer.join()
 
 
class Handler(FileSystemEventHandler):
    @staticmethod
    def on_any_event(event):
        if event.is_directory:
            return None
 
        elif event.event_type == 'created':
            # Take any action here when a file is first created.
            print("Received created event - %s." % event.src_path)
            timestamp = time.strftime("%Y-%m-%d_%H-%M-%S")
            command = "ffmpeg -i %s -s 1920x1080 -i /home/pi/Videos/tff_overlay_1920x1080.png -filter_complex overlay=0:0 -b 4000000 -q:v 1 /home/pi/Videos/converted_mp4/tff_logo_"% event.src_path+timestamp+".mp4"
            call([command], shell=True)
            sleep(.5)
            print("Deleting original MP4 file")
            os.unlink("%s"% event.src_path) 
            sleep(.5)
            print("Original MP4 file deleted")
            #run FTP transfer
            host = 'IP_ADDRESS'
            port = 21

            ftp = FTP()
            ftp.connect(host,port)
            ftp.login('user','password')
            filenameCV = "/home/pi/Videos/converted_mp4"
            def placeFiles(ftp, path):
                for name in os.listdir(path):
                    localpath = os.path.join(path, name)
                    if os.path.isfile(localpath):
                        print("STOR", name, localpath)
                        ftp.storbinary('STOR ' + name, open(localpath,'rb'))
                        os.unlink(localpath) 
                    elif os.path.isdir(localpath):
                        print("MKD", name)
            
                    try:
                        ftp.mkd(name)

                    # ignore "directory already exists"
                    except error_perm as e:
                        if not e.args[0].startswith('550'): 
                            raise

                    print("CWD", name)
                    ftp.cwd(name)
                    placeFiles(ftp, localpath)           
                    print("CWD", "..")
                    ftp.cwd("..") 

            placeFiles(ftp, filenameCV)
            ftp.quit()
            #End of FTP
 
 
 
if __name__ == '__main__':
    w = Watcher()
    w.run()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issues with Python script as service satishgunjal 8 7,471 Jun-21-2018, 11:40 AM
Last Post: satishgunjal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020