smtplib TimeoutError not catch
Joaquin Alzola
Joaquin.Alzola at lebara.com
Wed Oct 12 09:21:41 EDT 2016
Hi Guys
Try to connect to the smtp via smtplib.
The connection is down because the Firewall has not been open yet so the exception is something that should appear.
Now I want to catch this timeout in case error happens in future.
Here the exception trace.
[2016-10-12 14:14:06,289] ERROR in app: Exception on /api/barredMSISDN [POST]
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/root/main.py", line 34, in barredMSISDN
smtp_con(http_json)
File "/root/main.py", line 134, in smtp_con
server = smtplib.SMTP('smtp.lebara.com')
File "/usr/local/lib/python3.4/smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python3.4/smtplib.py", line 321, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/lib/python3.4/smtplib.py", line 292, in _get_socket
self.source_address)
File "/usr/local/lib/python3.4/socket.py", line 516, in create_connection
raise err
File "/usr/local/lib/python3.4/socket.py", line 507, in create_connection
sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out
How to catch such exception? ... have been trying with a lot of combinations (of course except the correct one :))
try:
server.sendmail(sender, receivers, msg.as_string())
except Exception as e: <-- have try with many combinations here
print("SMTP could not be contacted: %s",e)
pass
finally:
server.quit()
Joaquin
This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt.
More information about the Python-list
mailing list