How difference between ConnectionAbortedError, ConnectionRefusedError and ConnectionResetError and whether to catch an exception

259 views Asked by At

I'm working on the internet connection programming, including Server and Client Socket, FTP, SMTP. But there is an error that depending on internet connection condition that I must handle it. But I don't know what the ConnnectionAbortedError, ConnectionRefusedError and ConnectionResetError do. And whether to catch an exception. I definitely understand that TimeoutError is catch for the connection is not respond for long time then raise an error. But I can't identify what are the three connection errors on above do.

FTPManager.py module for using in another module.

from ftplib import *

blockSize = 1024

host = "localhost"
instance = None

def login():
  global instance
  while True:
    try:
      instance = FTP(host)
      instance.login(user="NetPro",passwd="12345678")
      instance.retrlines("LIST")
      instance.cwd("Final Assignment")
      print("FTP Connected!")
      break
    except TimeoutError:
      print("FTP Login Timeout Error. Retrying...")

def logout():
  global instance
  instance.quit()

def changeDirectory(directory):
  global instance
  instance.cwd(directory)
  instance.retrlines("LIST")

def listAllFiles():
  global instance
  return instance.nlst()

def isFileExists(filename):
  global instance
  fileList = instance.nlst()
  if filename in fileList:
    return True
  return False

def downloadFile(filename):
  while True:
    try:
      with open(filename,"wb") as f:
        instance.retrbinary("RETR " + filename, f.write, blockSize)
      print("Download file " + filename + " completed.")
      break
    except ConnectionRefusedError:
      print("FTP Connection has been aborted.")
      login()

def uploadFile(filename):
  while True:
    try:
      with open(filename,"rb") as f:
        instance.storbinary("STOR " + filename, f, blockSize)
      print("Upload file " + filename + " completed.")
      break
    except ConnectionAbortedError:
      print("FTP Connection has been aborted.")
      login()

login()

The error exception is also used in SMTPManager.py for handling the internet connection error.

Thank you. Any help is appreciated.

0

There are 0 answers