为什么我的图像通过 Python FTP 程序丢失数据?

Why is my Image losing data through my Python FTP program?

我的 Python 程序通过 FTP 将图像发送到我的网络服务器,但有时在到达时,传输图像的部分数据会丢失。该程序每隔 x 秒截取一次屏幕截图,然后将图像上传到网络服务器。

我的网络托管服务提供商认为它一定来自 Python 程序本身,所以请告诉我我做错了什么导致了这个问题。

图片(从网络服务器拉取时的样子):

代码:

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()

所以,真正的原因是我在实际传输完成之前从网络服务器打开了 FTP 图片。我的解决方案是向我的 Web 服务器添加一个 PHP 过滤器,以便仅在图像超过特定大小时才拉取图像,以避免在整个传输完成之前过早查看文件。

它现在运行良好,我很高兴这个问题是一个简单的程序修复