Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @contextmanager
- def smtp_connection(host, user=None, passwd=None, timeout=5):
- conn = None # smell here
- try:
- conn = SMTP(host=host, timeout=timeout)
- conn.ehlo_or_helo_if_needed()
- if user and passwd:
- conn.login(user=user, password=passwd)
- logger.debug('SMTP connected')
- yield conn
- except Exception as e:
- raise e
- finally:
- if conn: # and here
- conn.quit()
- with ExitStack() as stack:
- stack.callback(cleanup_resources)
- result = perform_operation()
- if result:
- stack.pop_all()
- with ExitStack() as stack:
- result = None
- stack.callback(lambda conn: conn.quit())
- result = POP3() # code from above here
- if result:
- stack.pop_all()
- except Exception as e:
- raise e
- except Exception:
- raise
- @contextmanager
- def smtp_connection(host, user=None, passwd=None, timeout=5):
- conn = SMTP(host=host, timeout=timeout)
- try:
- conn.ehlo_or_helo_if_needed()
- if user and passwd:
- conn.login(user=user, password=passwd)
- logger.debug('SMTP connected')
- yield conn
- finally:
- conn.quit()
- @contextmanager
- def smtp_connection(host, timeout):
- connection = SMTP(host=host, timeout=timeout)
- yield connection
- connection.quit()
- @contextmanager
- def smtp_setup(host, user=None, passwd=None, timeout=5):
- with smtp_connection(host, timeout) as conn:
- conn.ehlo_or_helo_if_needed()
- if user and passwd:
- conn.login(user=user, password=passwd)
- logger.debug('SMTP connected')
- yield conn
- @contextmanager
- def smtp_connection(host, timeout):
- connection = SMTP(host=host, timeout=timeout)
- yield connection
- connection.quit()
- def smtp_setup(conn, user=None, passwd=None):
- conn.ehlo_or_helo_if_needed()
- if user and passwd:
- conn.login(user=user, password=passwd)
- logger.debug('SMTP connected')
- with smtp_connection(h, u, p, t) as conn:
- # do stuff
- with smtp_connection(h, t) as conn:
- smtp_setup(conn, u, p)
- # do stuff
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement