在python 3中关闭ssh隧道但在python 2中没有关闭的问题

问题描述 投票:0回答:1

早上好。

我正在尝试通过SSH隧道读取带有pandas的SQL查询。它在python 2.7中运行良好,但是现在,使用python 3.7,似乎在尝试关闭隧道时暂停了进程。我的代码如下:

def conect(lista, names):

    # Block of code where I set used variables below.

    while not success and retries < max_retries:

        try:
            print('Trying to connect ({n})...'.format(n = retries + 1))
            sshtunnel.DEFAULT_LOGLEVEL = logging.DEBUG

            with SSHTunnelForwarder((REMOTE_SERVER, 22),
                                ssh_username = user_name,
                                ssh_pkey     = ssh_pkey,
                                ssh_private_key_password= password,
                                remote_bind_address=(str_number, PUERTO),
                                local_bind_address=('', PUERTO)) as tunnel:

                engine = sqlalchemy.create_engine("postgresql+psycopg2://{user}:{passw}@{host}:{port}/{db}".format(
                        user  = user_name,
                        passw = long_pass,
                        host  = tunnel.local_bind_host,
                        port  = tunnel.local_bind_port,
                        db    = db))

                conn     = engine.connect()
                dic_df   = {name: pd.DataFrame(conn.execute(query).fetchall(), columns = conn.execute(query).keys()) for (query, name) in zip(lista, names)}


            return dic_df

        except Exception as e:
            print('Error...')
            print(e)
        retries += 1

我通过调试模式获得的日志是:

Python 2

2019-04-03 16:12:02,563 | WAR | MainThrea / 0967 @ sshtunnel |无法读取SSH配置文件:〜/ .ssh / config

2019-04-03 16:12:02,564 | INF | MainThrea / 0993 @sshtunnel |从代理加载0键

2019-04-03 16:12:02,564 | INF | MainThrea / 1042 @ sshtunnel |从主机目录加载0个密钥

2019-04-03 16:12:02,674 | DEB | MainThrea / 1229 @ sshtunnel |私钥文件(/ Users / agarzon / Desktop / id_rsa)已成功加载

2019-04-03 16:12:02,675 | INF | MainThrea / 0914 @sshtunnel |连接到网关:REMOTE SERVER:22作为用户user_name

2019-04-03 16:12:02,675 | DEB | MainThrea / 0917 @ sshtunnel |允许并发连接:正确

2019-04-03 16:12:02,675 | DEB | MainThrea / 1355 @ sshtunnel |尝试使用密钥登录:240aa5925ca5e09b3c905a48202bcfe2

2019-04-03 16:12:02,690 | WAR | Thread-1/0368 @ ec | /Library/Python/2.7/site-packages/paramiko/kex_ecdh_nist.py:39:ColptographyDeprecationWarning:encode_point已在EllipticCurvePublicNumbers上弃用,将在未来版本中删除。请使用EllipticCurvePublicKey.public_bytes获取压缩和未压缩的点编码。 m.add_string(self.Q_C.public_numbers()。encode_point())

2019-04-03 16:12:02,728 | WAR | Thread-1/0387 @ ec | /Library/Python/2.7/site-packages/paramiko/kex_ecdh_nist.py:96:CryptographyDeprecationWarning:在未来版本中将删除对编码数据中不安全构建公共号码的支持。请使用EllipticCurvePublicKey.from_encoded_point self.curve,Q_S_bytes

2019-04-03 16:12:02,730 | WAR | Thread-1/0368 @ ec | /Library/Python/2.7/site-packages/paramiko/kex_ecdh_nist.py:111:CryptographyDeprecationWarning:encode_point已在EllipticCurvePublicNumbers上弃用,将在以后的版本中删除。请使用EllipticCurvePublicKey.public_bytes获取压缩和未压缩的点编码。 hm.add_string(self.Q_C.public_numbers()。encode_point())

2019-04-03 16:12:02,825 | INF | Srv-5432/1389 @ sshtunnel |开放隧道:0.0.0.0:PUERTO <> str_number:PUERTO

2019-04-03 16:12:03,495 | INF | MainThrea / 1408 @ sshtunnel |关闭隧道('0.0.0.0',PUERTO)

2019-04-03 16:12:03,588 | INF | Srv-5432/1395 @ sshtunnel |隧道:0.0.0.0:PUERTO <> str_number:PUERTO发布

2019-04-03 16:12:03,597 | DEB | MainThrea / 1422 @ sshtunnel |运输已结束

工作得很好......

Python 3

2019-04-03 16:16:07,326 | WAR | MainThrea / 0967 @ sshtunnel |无法读取SSH配置文件:〜/ .ssh / config

2019-04-03 16:16:07,327 | INF | MainThrea / 0993 @sshtunnel |从代理加载0键

2019-04-03 16:16:07,327 | INF | MainThrea / 1042 @ sshtunnel |从主机目录加载0个密钥

2019-04-03 16:16:07,414 | DEB | MainThrea / 1229 @ sshtunnel |私钥文件(/ Users / agarzon / Desktop / id_rsa)已成功加载

2019-04-03 16:16:07,414 | INF | MainThrea / 0914 @sshtunnel |连接到网关:REMOTE SERVER:22作为用户user_name

2019-04-03 16:16:07,414 | DEB | MainThrea / 0917 @ sshtunnel |允许并发连接:正确

2019-04-03 16:16:07,415 | DEB | MainThrea / 1355 @ sshtunnel |尝试使用密钥登录:b'240aa5925ca5e09b3c905a48202bcfe2'

2019-04-03 16:16:07,431 | WAR | Thread-1/0110 @ warnings | /usr/local/lib/python3.7/site-packages/paramiko/kex_ecdh_nist.py:39:ColptographyDeprecationWarning:encode_point已在EllipticCurvePublicNumbers上弃用,将在以后的版本中删除。请使用EllipticCurvePublicKey.public_bytes获取压缩和未压缩的点编码。 m.add_string(self.Q_C.public_numbers()。encode_point())

2019-04-03 16:16:07,474 | WAR | Thread-1/0110 @ warnings | /usr/local/lib/python3.7/site-packages/paramiko/kex_ecdh_nist.py:96:CryptographyDeprecationWarning:将来版本中将删除对编码数据中不安全构建公共号码的支持。请使用EllipticCurvePublicKey.from_encoded_point self.curve,Q_S_bytes

2019-04-03 16:16:07,476 | WAR | Thread-1/0110 @ warnings | /usr/local/lib/python3.7/site-packages/paramiko/kex_ecdh_nist.py:111:CryptographyDeprecationWarning:encode_point已在EllipticCurvePublicNumbers上弃用,将在以后的版本中删除。请使用EllipticCurvePublicKey.public_bytes获取压缩和未压缩的点编码。 hm.add_string(self.Q_C.public_numbers()。encode_point())

2019-04-03 16:16:07,542 | INF | Srv-5432/1389 @ sshtunnel |开放隧道:0.0.0.0:PUERTO <> str_number:PUERTO

2019-04-03 16:16:08,184 | INF | MainThrea / 1408 @ sshtunnel |关闭隧道('0.0.0.0',PUERTO)

2019-04-03 16:16:08,229 | INF | Srv-5432/1395 @ sshtunnel |隧道:0.0.0.0:PUERTO <> str_number:PUERTO发布

这几乎是相同的,但是,你可以检查,不要停止与隧道的连接。另外,如果在设置dic_df的值后断开代码,你可以检查它是否正常工作,所以我很确定失败来自停止隧道......

提前谢谢了!!

pandas postgresql python-3.7
1个回答
0
投票

基于我的评论,这里是我如何使用psycopg2的一个例子。

with SSHTunnelForwarder((SSH_HOST, 22),
                         ssh_username=SSH_USER,
                         ssh_password=SSH_PW,
                         remote_bind_address=('localhost', SSH_FOREIGN_PORT),
                         local_bind_address=('localhost', SSH_INTERNAL_PORT)
                         ) as server:

        with psycopg2.connect(host=server.local_bind_host,
                              port=server.local_bind_port,
                              dbname=DB_DATABASE,
                              user=DB_USER,
                              password=DB_PASSWORD
                              ) as conn:
© www.soinside.com 2019 - 2024. All rights reserved.