Pandas 在Python3中关闭ssh隧道时出现问题,但在Python2中没有

Pandas 在Python3中关闭ssh隧道时出现问题,但在Python2中没有,pandas,postgresql,python-3.7,Pandas,Postgresql,Python 3.7,早上好 我正试图通过SSH隧道读取带有pandas的SQL查询。它在Python2.7中工作得很好,但是现在,在Python3.7中,当试图关闭隧道时,进程似乎暂停了。我的代码如下: def conect(lista, names): # Block of code where I set used variables below. while not success and retries < max_retries: try:

早上好

我正试图通过SSH隧道读取带有pandas的SQL查询。它在Python2.7中工作得很好,但是现在,在Python3.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:02563 |战争|主要地区/0967@sshtunnel|无法读取SSH配置文件:~/.SSH/config

2019-04-03 16:12:02564 INF MainThrea/0993@sshtunnel|从代理加载了0个密钥

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

2019-04-03 16:12:02674主要地区/1229@sshtunnel|私钥文件/Users/agarzon/Desktop/id_rsa,已成功加载

2019-04-03 16:12:02675 INF主区域/0914@sshtunnel|连接到网关:远程服务器:22作为用户名

2019-04-03 16:12:02675主要地区/0917@sshtunnel|允许并发连接:True

2019-04-03 16:12:02675主要地区/1355@sshtunnel|尝试使用密钥240aa5925ca5e09b3c905a48202bcfe2登录

2019-04-03 16:12:02690战争线程1/0368@ec|/Library/Python/2.7/site packages/paramiko/kex_ecdh_nist.py:39:cryptographydeproductionwarning:encode_point已在EllipticCurvePublicNumber上弃用,并将在未来版本中删除。请使用EllipticCurvePublicKey.public_字节获取压缩和未压缩的点编码。 m、 添加\u stringself.Q\u C.public\u number.encode\u point

2019-04-03 16:12:02728 |战争|线程-1/0387@ec|/Library/Python/2.7/site packages/paramiko/kex_ecdh_nist.py:96:cryptographydeproduction警告:在未来的版本中,将删除对编码数据中不安全构造公共数字的支持。请使用EllipticCurvePublicKey.from_encoded_点 self.curve,Q_S_字节

2019-04-03 16:12:02730 |战争|线程-1/0368@ec|/Library/Python/2.7/site packages/paramiko/kex_ecdh_nist.py:111:cryptographydeproductionwarning:encode_point已在EllipticCurvePublicNumber上弃用,并将在未来版本中删除。请使用EllipticCurvePublicKey.public_字节获取压缩和未压缩的点编码。 hm.add_stringself.Q_C.public_number.encode_point

2019-04-03 16:12:02825 | INF | Srv-5432/1389@sshtunnel|开放隧道:0.0.0.0:波多黎各街|编号:波多黎各

2019-04-03 16:12:03495 INF主区域/1408@sshtunnel|关闭波多黎各“0.0.0.0”号隧道

2019-04-03 16:12:03588 | INF | Srv-5432/1395@sshtunnel|隧道:0.0.0.0:波多黎各街|编号:波多黎各发布

2019-04-03 16:12:03597主要地区/1422@sshtunnel|交通关闭

这很好用

Python 3

2019-04-03 16:16:07326 |战争|主要地区/0967@sshtunnel|无法读取SSH配置文件:~/.SSH/config

2019-04-03 16:16:07327 INF主区域/0993@sshtunnel|从代理加载了0个密钥

2019-04-03 16:16:07327 INF主区域/1042@sshtunnel|从主机目录加载0个密钥

2019-04-03 16:16:07414主要地区/1229@sshtunnel|私钥文件/Users/agarzon/Desktop/id_rsa,已成功加载

2019-04-03 16:16:07414 INF主区域/0914@sshtunnel|连接到网关:远程服务器:22作为用户名

2019-04-03 16:16:07414主要地区/0917@sshtunnel|允许并发连接:True

2019-04-03 16:16:07415主城区/1355@sshtunnel|尝试使用密钥b'240aa5925ca5e09b3c905a48202bcfe2'登录

2019-04-03 16:16:07431 |战争|线程-1/0110@warnings|/usr/local/lib/python3.7/site packages/paramiko/kex_ecdh_nist.py:39:cryptographydeproductionwarning:encode_point已在EllipticcurvePublicNumber上被弃用,并将在未来版本中删除。请使用EllipticCurvePublicKey.public_字节获取压缩和未压缩的点编码。 m、 添加\u stringself.Q\u C.public\u number.encode\u point

2019-04-03 16:16:07474 |战争|线程-1/0110@warnings|/usr/local/lib/python3.7/site packages/paramiko/kex_ecdh_nist.py:96:CryptographydeproductionWarning:在未来的版本中,将删除对编码数据中公共数字的不安全构造的支持。请使用EllipticCurvePublicKey.from_encoded_点 self.curve,Q_S_字节

2019-04-03 16:16:07476 |战争|线程-1/0110@warnings|/usr/local/lib/python3.7/site packages/paramiko/kex_ecdh_nist.py:111:cryptographydeproductionwarning:encode_point已在EllipticcurvePublicNumber上被弃用,并将在未来版本中删除。请使用EllipticCurvePublicKey.public_字节获取压缩和未压缩的点编码。 hm.add_stringself.Q_C.public_number.encode_point

2019-04-03 16:16:07542-INF-Srv-5432/1389@sshtunne l|开放隧道:0.0.0.0:PUERTO街编号:PUERTO

2019-04-03 16:16:08184 INF主区域/1408@sshtunnel|关闭波多黎各“0.0.0.0”号隧道

2019-04-03 16:16:08229-INF-Srv-5432/1395@sshtunnel|隧道:0.0.0.0:波多黎各街|编号:波多黎各发布

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


非常感谢

根据我的评论,这里是我如何使用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:

也许引擎还在连接,它拒绝关闭隧道。可能尝试使用engine.connect as conn:或尝试显式关闭conn。我不使用sqlalchemy,我使用psycopg2,但我知道我的连接块使用的是,就像SSHTunnelForwarder一样。我使用了与Python3.6非常相似的设置谢谢你的回答,这是一个很好的观点。我不知道为什么,但我在使用psycopg2时遇到了问题,所以我尝试将您的解决方案改为我的代码,替换为conn=engine.connect with with with engine.connect as conn:,但我的问题仍然存在……对不起,我想我没有更多的东西可以提供。我只知道在使用Linux时,如果您打开了与隧道相关的进程,它将一直挂起,直到连接关闭。例如,使用SSH隧道将端口链接到某个外部服务器。然后,使用另一个与外部服务器有开放连接的应用程序(如DBeaver),必须断开DBeaver连接,然后隧道将关闭。