次の関数を使用すると、with-statmentおよびa pop接続を使用できます。しかし、接続が確立されていない場合、quit()は最後に例外を発生させます。どのようにこれを修正することができますか?未接続の接続およびコンテンツマネージャー
@contextmanager
def pop_connect(server, user, password, timeout, use_SSL=False):
try:
pop = poplib.POP3_SSL if use_SSL else poplib.POP3
pop_conn = pop(server, timeout=timeout)
pop_conn.pass_(password)
yield pop_conn
except poplib.error_proto as pop_error:
print('Authentication for receiving emails failed:{}'.format(pop_error))
except OSError as os_error:
print('Name resolution or connection failed:{}'.format(os_error))
finally:
pop_conn.quit()