sql >> Database >  >> RDS >> Mysql

SSH-tunnel van Python wordt automatisch gesloten

Volgens steven-rumbalski 's opmerking hierboven:

Vervangen: with SSHTunnelForwarder(...) as server
Met: server = SSHTunnelForwarder(...)
Wikkel vervolgens: server.start() ... server.stop()
rond de code die u via de SSH-tunnel wilt laten verzenden.

Hier is de geconverteerde code:

import pymysql, shlex, shutil, subprocess
import logging
import sshtunnel
from sshtunnel import SSHTunnelForwarder
import iot_config as cfg

def OpenSSHTunnel():
    global server
    sshtunnel.DEFAULT_LOGLEVEL = logging.DEBUG
    server = SSHTunnelForwarder(
        (cfg.sshconn['host'], cfg.sshconn['port']),
        ssh_username = cfg.sshconn['user'],
        ssh_private_key = cfg.sshconn['private_key_loc'],
        ssh_private_key_password = cfg.sshconn['private_key_passwd'],
        remote_bind_address = ('127.0.0.1', 3306)
    )

def OpenRemoteDB():
    global remotecur, remotedb
    remotedb = None
    remotedb = pymysql.connect(
        host='127.0.0.1',
        user=cfg.remotedbconn['user'],
        passwd=cfg.remotedbconn['passwd'],
        db=cfg.remotedbconn['db'],
        port=server.local_bind_port
    )
    remotecur = remotedb.cursor()
    print("Checkpoint 1")

def SyncActions():
    print("Checkpoint 2")
    # this should now work as expected
    remotecur.execute("SELECT ActionID, Description FROM cmAction")
    for r in remotecur:
        print(r)

# Main program starts here
OpenSSHTunnel()
server.start()
OpenRemoteDB()
SyncActions()
server.stop()



  1. Gebruik MySQL relationele databases op CentOS 5.

  2. Fixing Lock wacht time-out overschreden; probeer de transactie opnieuw te starten voor een 'vastgelopen Mysql-tabel?

  3. Update ListView op basis van door SQLite ondersteunde ContentProvider

  4. geef het originele rijnummer van mySQL terug