Advertisement
Guest User

Untitled

a guest
Apr 20th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. @contextmanager
  2. def smtp_connection(host, user=None, passwd=None, timeout=5):
  3. conn = None # smell here
  4. try:
  5. conn = SMTP(host=host, timeout=timeout)
  6. conn.ehlo_or_helo_if_needed()
  7. if user and passwd:
  8. conn.login(user=user, password=passwd)
  9. logger.debug('SMTP connected')
  10. yield conn
  11. except Exception as e:
  12. raise e
  13. finally:
  14. if conn: # and here
  15. conn.quit()
  16.  
  17. with ExitStack() as stack:
  18. stack.callback(cleanup_resources)
  19. result = perform_operation()
  20. if result:
  21. stack.pop_all()
  22.  
  23. with ExitStack() as stack:
  24. result = None
  25. stack.callback(lambda conn: conn.quit())
  26. result = POP3() # code from above here
  27. if result:
  28. stack.pop_all()
  29.  
  30. except Exception as e:
  31. raise e
  32.  
  33. except Exception:
  34. raise
  35.  
  36. @contextmanager
  37. def smtp_connection(host, user=None, passwd=None, timeout=5):
  38. conn = SMTP(host=host, timeout=timeout)
  39. try:
  40. conn.ehlo_or_helo_if_needed()
  41. if user and passwd:
  42. conn.login(user=user, password=passwd)
  43. logger.debug('SMTP connected')
  44. yield conn
  45. finally:
  46. conn.quit()
  47.  
  48. @contextmanager
  49. def smtp_connection(host, timeout):
  50. connection = SMTP(host=host, timeout=timeout)
  51. yield connection
  52. connection.quit()
  53.  
  54. @contextmanager
  55. def smtp_setup(host, user=None, passwd=None, timeout=5):
  56. with smtp_connection(host, timeout) as conn:
  57. conn.ehlo_or_helo_if_needed()
  58. if user and passwd:
  59. conn.login(user=user, password=passwd)
  60. logger.debug('SMTP connected')
  61. yield conn
  62.  
  63. @contextmanager
  64. def smtp_connection(host, timeout):
  65. connection = SMTP(host=host, timeout=timeout)
  66. yield connection
  67. connection.quit()
  68.  
  69. def smtp_setup(conn, user=None, passwd=None):
  70. conn.ehlo_or_helo_if_needed()
  71. if user and passwd:
  72. conn.login(user=user, password=passwd)
  73. logger.debug('SMTP connected')
  74.  
  75. with smtp_connection(h, u, p, t) as conn:
  76. # do stuff
  77.  
  78. with smtp_connection(h, t) as conn:
  79. smtp_setup(conn, u, p)
  80. # do stuff
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement