同样的代码,用不同的运行版本结果会不同是什么原因造成的?
如题所说,小白一枚,写了一组pytho代码,基本上实现的就是通过sshtunnel, pymysql远程连接数据库查询并获取数据。
若运行环境为python2.7时没问题,但换成python3.8就会一直不返回查询结果(也不报错,长时间无输出,直至强制停止运行),连接数据库的代码大至如下,有没有大神帮忙看一下是为什么?
import pymysql as MySQLdb
from sshtunnel import SSHTunnelForwarder
def runSQLDB1(sql):
with SSHTunnelForwarder(
('11.11.11.11',2222),
ssh_username="$user",
ssh_password="$pass",
# ssh_pkey="/home/kk/key.pem" ,
remote_bind_address=('$address',1111)
) as server1:
DB1 = MySQLdb.connect(host='127.0.0.1',
port=server1.local_bind_port,
user='$user',
password='$pass',
db='$db',
charset='utf8',
cursorclass=MySQLdb.cursors.DictCursor)
try:
CUSOR = DB1.cursor()
CUSOR.execute(sql)
result = CUSOR.fetchall()
except BaseException as e:
print(e)
finally:
CUSOR.close()
return result