简体   繁体   中英

Cannot close SSH connection in python

I am new in python so I may have messed up some things. I am connecting to MySql but after doing the select I want to exit the connection, I tried using server.stop() but that did not work.

from sshtunnel import SSHTunnelForwarder
import getpass
import mysql.connector


#password_ssh = getpass.getpass("Insert Password of SSH Connection")
#password_db = input("Insert Password of MySql DB")
user_ssh = 'user'

user_db = 'user'

with SSHTunnelForwarder(
        ('{host}', port),
        ssh_password= '',
        ssh_username= user_ssh,
        remote_bind_address=('127.0.0.1', 3306)) as server:
    con = mysql.connector.connect(
        user=user_db, passwd='',
        db='db_name', host='127.0.0.1',
        port=server.local_bind_port)
    mycursor = con.cursor()

    mycursor.execute("SELECT.... ")
    myresult = mycursor.fetchall()
    server.stop()
print(myresult)

I am not able to print the value of the variable: myresult, the variable is not null and if I put it inside the 'with SSH..' it prints, but if I put it outside it does not work. Am I missing somethings or the indentation is not correct ?

Thank you

This happens because your client is still open.

It will allow you to stop server, once you closed your client.

con.close()

server.stop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM