I tried to write an email using python's smtplib module. Here is my code as well as the error displayed below. Thank you!
import smtplib
my_email = 'xxxxxxxxxxx#hotmail.com'
password = 'xxxxxxxxxx'
# I hid my email and password here
with smtplib.SMTP('outlook.office365.com') as connection:
connection.starttls()
connection.login(user=my_email, password=password)
connection.sendmail(from_addr=my_email, to_addrs=my_email, msg='helloworld')
/usr/local/bin/python3 "/Users/guanhongjiang/PycharmProjects/pythonProject/Birthday Wisher (Day 32) start/main.py"
Traceback (most recent call last):
File "/Users/guanhongjiang/PycharmProjects/pythonProject/Birthday Wisher (Day 32) start/main.py", line 6, in <module>
with smtplib.SMTP('outlook.office365.com') as connection:
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socket.py", line 845, in create_connection
raise err
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socket.py", line 833, in create_connection
sock.connect(sa)
TimeoutError: [Errno 60] Operation timed out
I am trying to send emails using python and smptplib but it shows some errors the following is my code:
import smtplib,ssl
port = 456
password = input("enter your password : ")
email_id = input("enter your email adress: ")
print("creating connection...")
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com",port,context=context) as server:
server.login(email_id,password)
Traceback (most recent call last):
File "/Users/rijilvarghese/Documents/email.py/newemail.py", line 9, in <module>
with smtplib.SMTP_SSL("smtp.gmail.com",port,context=context) as server:
File "/opt/anaconda3/lib/python3.7/smtplib.py", line 1031, in __init__
source_address)
File "/opt/anaconda3/lib/python3.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/opt/anaconda3/lib/python3.7/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/opt/anaconda3/lib/python3.7/smtplib.py", line 1037, in _get_socket
self.source_address)
File "/opt/anaconda3/lib/python3.7/socket.py", line 727, in create_connection
raise err
File "/opt/anaconda3/lib/python3.7/socket.py", line 716, in create_connection
sock.connect(sa)
OSError: [Errno 65] No route to host
this Are the errors,What should i do?
I've got a redshift instance that is accessible by a bastion host.
I'm trying to trigger a circle CI job that alters a db schema to be renamed to include a git release tag version. We want this to be triggered when we create the git release targeted against master.
The CI workflow does the following:
1) Authenticate with AWS
2) Run python script that creates a security group, attaches it to a bastion host, and configures ingress rules to allow the CI box's IP to be whitelisted as a cidr.
3) Open an ssh tunnel to forward sqlalchemy commands thru the bastion to the redshift host.
4) Run another python script that alters the db schema
The first two steps are not having any problems. The problem seems to be occurring with the ssh tunnel.
ssh tunnel running in background and calling python script:
ssh -o StrictHostKeyChecking=no -N -L 127.0.0.1:5439:$PRODUCTION_REDSHIFT_DNS:5439 -i /home/circleci/.ssh/id_rsa_d88b40ac655a406a2c26ef75202fc144 circle_ci#$PRODUCTION_BASTION_DNS & python3 archive_schema.py
$PROD_REDSHIFT_CONNECTION_STRING: redshift+psycopg2://$USER:$PASS#localhost:5439/$DB_NAME
I've got a simple python script that prints the first table name in the database and exits. I ran it in the bastion successfully with the same connection string, but when I run it after setting up the ssh tunnel in the background things blow up.
import os
from sqlalchemy import create_engine, inspect
engine = create_engine(os.getenv("PROD_REDSHIFT_CONNECTION_STRING"))
inspector = inspect(engine)
for table_name in inspector.get_table_names():
print(table_name)
break
Any help would be greatly greatly appreciated. Here's the error that I get when running the script from circle CI:
Traceback (most recent call last):
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2262, in _wrap_pool_connect
return fn()
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 303, in unique_connection
return _ConnectionFairy._checkout(self)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
rec = pool._do_get()
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 139, in _do_get
self._dec_overflow()
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 154, in reraise
raise value
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
return self._create_connection()
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
return _ConnectionRecord(self)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
self.__connect(first_connect_check=True)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 639, in __connect
connection = pool._invoke_creator(self)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 451, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5439?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5439?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5439?
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "z.py", line 7, in <module>
inspector = inspect(engine)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/inspection.py", line 63, in inspect
ret = reg(subject)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/engine/reflection.py", line 141, in _insp
return Inspector.from_engine(bind)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/engine/reflection.py", line 136, in from_engine
return bind.dialect.inspector(bind)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/dialects/postgresql/base.py", line 2209, in __init__
reflection.Inspector.__init__(self, conn)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/engine/reflection.py", line 111, in __init__
bind.connect().close()
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2193, in connect
return self._connection_cls(self, **kwargs)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 103, in __init__
else engine.raw_connection()
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2293, in raw_connection
self.pool.unique_connection, _connection
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2266, in _wrap_pool_connect
e, dialect, self
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1536, in _handle_dbapi_exception_noconnection
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 399, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
raise value.with_traceback(tb)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2262, in _wrap_pool_connect
return fn()
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 303, in unique_connection
return _ConnectionFairy._checkout(self)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
rec = pool._do_get()
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 139, in _do_get
self._dec_overflow()
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 154, in reraise
raise value
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
return self._create_connection()
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
return _ConnectionRecord(self)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
self.__connect(first_connect_check=True)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 639, in __connect
connection = pool._invoke_creator(self)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 451, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/home/circleci/repo/venv/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5439?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5439?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5439?
(Background on this error at: http://sqlalche.me/e/e3q8)
Exited with code 1
This could be happening because the first command has not established the tunnel when the second command was ran. You should avoid using & and use -f in your ssh tunnel command.
Also one can also say there is no way to pass custom view function to change or reset password so that any exceptions occurred can be handled.
I my case I have set SECURITY_SEND_PASSWORD_CHANGE_EMAIL and SECURITY_SEND_PASSWORD_RESET_EMAIL to True. However if SMTP is not configured then changing or resetting password fails very badly in debug mode. (see below exception)
And in case of production mode no response is sent to http request.
Instead I want to handle such exceptions and show proper error message to user (like SMTP not configured)
File "/lib/python3.4/site-packages/flask_security/utils.py", line 341, in send_mail
mail.send(msg)
File "/lib/python3.4/site-packages/flask_mail.py", line 491, in send
with self.connect() as connection:
File "/lib/python3.4/site-packages/flask_mail.py", line 144, in __enter__
self.host = self.configure_host()
File "/lib/python3.4/site-packages/flask_mail.py", line 158, in configure_host
host = smtplib.SMTP(self.mail.server, self.mail.port)
File "/usr/lib/python3.4/smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.4/smtplib.py", line 321, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.4/smtplib.py", line 292, in _get_socket
self.source_address)
File "/usr/lib/python3.4/socket.py", line 512, in create_connection
raise err
File "/usr/lib/python3.4/socket.py", line 503, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
I'm running a Mac OS X and used postfix to enable localhost.
This is the error message I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 61] Connection refused
Use netstat to verify that a SMTP server is bound to port 25 of an appropriate interface in the first place.
Check the postfix log file, which might be at /var/log/mail or something similar, depending on how you installed postfix.