-2

My Python program sends an image to my web server through FTP, but occasionally upon arrival, partial data is lost from the transferred image. The program takes a screenshot every x amount of seconds and then uploads the image to the web server.

My web hosting provider thinks it must be coming from the Python program itself, so please let me know what I'm doing wrong to cause this issue.

Image(What it looks like when pulled from the web server): enter image description here

Code:

def ftp(self):  # Screen Grab and FTP Transfer

    new = ImageGrab.grab(bbox=(0, 50, 1366, 720))
    new = new.resize((1366, 700), PIL.Image.ANTIALIAS)
    new.save("C:\\Users\\Owner\\Desktop\\screenshots\\capture.jpg")

    newOpen = PIL.Image.open("C:\\Users\\user\\Desktop\\screenshots\\capture.jpg")
    newOpen.save("C:\\Users\\Owner\\Desktop\\screenshots\\capture.jpg", format="JPEG", quality=40)

    tries = 10  # Denotes maximum try limit for retry attempts

    for i in range(tries):
        try:

            # FTP image to Web Server
            session = ftplib.FTP('server', 'user', 'pass')
            file = open('C:\\Users\\Owner\\Desktop\\screenshots\\capture.jpg', 'rb')  # file to send
            session.storbinary('STOR capture.jpg', file)  # send the file
            file.close()  # close file and FTP
            session.quit()

            value = "Updated. \nFailed " + str(i) + " Times\n" + str(self.tick)

            print value

            self.tick += 1

        except KeyError as e:

            if i < tries - 1:  # i is zero indexed

                continue

            else:

                raise

        break

    threading.Timer(5, self.ftp).start()
jameson1128
  • 135
  • 2
  • 13
  • Did you check size of the file before and after upload? Do they match? – Martin Prikryl Apr 22 '17 at 06:42
  • @MartinPrikryl Credit is due to you because of your suggestion. By monitoring the file size, I was able to diagnose the issue. Thanks for the assistance. Also, huge fan of your software – jameson1128 Apr 24 '17 at 19:24

1 Answers1

2

So, the actual reason for this was due to me opening the FTP image from the web server before the actual transfer was complete. My solution was to add a PHP filter to my web server to only pull the image when it was above a specific size as to avoid the premature viewing of the file before the whole transfer completed.

It works perfectly now and I am glad that the problem was a simple programmatic fix

jameson1128
  • 135
  • 2
  • 13