On 2016-10-12 14:21, Joaquin Alzola wrote:
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()
Not surprisingly, the exception you should catch is simply TimeoutError:
try:
server.sendmail(sender, receivers, msg.as_string())
except TimeoutError as e:
print("SMTP could not be contacted: %s" % e)
finally:
server.quit()
--
https://mail.python.org/mailman/listinfo/python-list