python 3.x - How to handle the Loss connection error and BaseSSHTunnelForwarderError: Could not establish session to SSH gateway -
this code shown below use connect database , tables. have used while without problem keep on getting 2 errors after runs few times. assume database. happens can connect databases, retrieve table since data huge on loop loop through dates within given range , data. example start 20160401 , count 1 one until 20170331(about 365 loops). after run 20 loops error s
operationerror "lost connection mysql server during query (%s)" % (e,))
and 1 below
basesshtunnelforwardererror: not establish session ssh gateway
i solve errors if not possible wait few seconds reconnect databases automatically execute same query. in solving highly appreciated.
import pymysql sshtunnel import sshtunnelforwarder sshtunnelforwarder( (ssh_host, ssh_port), ssh_username=ssh_user, ssh_pkey=mypkey, remote_bind_address=(sql_ip, sql_port)) tunnel: conn = pymysql.connect(host='127.0.0.1', user=sql_username, passwd=sql_password, port=tunnel.local_bind_port, db=db,charset='ujis') data = pd.read_sql_query(query_2, conn,) this have tried far not working
while true: try: sshtunnelforwarder( (ssh_host, ssh_port), ssh_username=ssh_user, ssh_pkey=mypkey, remote_bind_address=(sql_ip, sql_port)) tunnel: conn = pymysql.connect(host='127.0.0.1', user=sql_username, passwd=sql_password, port=tunnel.local_bind_port, db=db, charset='ujis') data = pd.read_sql_query(query, conn) break except pymysql.err.operationalerror e: conn.ping(reconnect=true)
Comments
Post a Comment