I have a Python deamon process that get frecquently some information to process and send result to FTP. When starting the process, everything works fine. However, after a while (can't say how long), I have a "ERROR 32 Broken Pipe" error. I understand that the error can also come from awaking time, and that I cannot keep a connection alive for too long (berfore being closed by any parties). But I can't find how to restore the connection
Here is a basic example of my current process and how I'm dealing with pipe being broken
class MediaCollector:
"""
Class to collect media from a given url and store it in a FTP folder
"""
def __init__(self, url, username, password, SSL=False) -> None:
self.username = username
self.password = password
self.url = url
self.SSL = SSL
if SSL:
self.session = ftplib.FTP_TLS(url,username,password)
else:
self.session = ftplib.FTP(url,username,password)
pass
def get_media(self, media_url, media_name) -> bool :
"""
Get media from a given url and store it in a FTP folder
Args:
media_url (str): url to get media from
media_name (str): name of the media file
"""
try:
self.session.storbinary('STOR '+media_name, requests.get(media_url, stream=True).raw)
return True
except Exception as e:
print('Error getting media: ' + str(e))
print('retrying...')
time.sleep(5)
try:
self.reconnect()
self.session.storbinary('STOR '+media_name, requests.get(media_url, stream=True).raw)
return True
except Exception as e:
# final retry, closing the connection and opening a new one
try:
self.session.quit()
if self.SSL:
self.session = ftplib.FTP_TLS(self.url,self.username,self.password)
else:
self.session = ftplib.FTP(self.url,self.username,self.password)
self.session.storbinary('STOR '+media_name, requests.get(media_url, stream=True).raw)
return True
except:
print('Error getting media: ' + str(e))
traceback.print_exc()
def reconnect(self):
return self.session.login(self.username, self.password)
I admit that the codes is not the pretier we can get, but still I hoped it would work... but not. What am I missing here ?