Forums

Experiencing problems during connection Python ->MySQL form outside.

Hello, I have a paid plan and en error. Maybe you can give me an idea whats going wrong.

from flask import Flask
import MySQLdb
import sshtunnel

# Set SSH and Tunnel timeouts
sshtunnel.SSH_TIMEOUT = 2.0
sshtunnel.TUNNEL_TIMEOUT = 2.0

app = Flask(__name__)

with sshtunnel.SSHTunnelForwarder(
        ('ssh.eu.pythonanywhere.com'),
       ssh_username='pythonanywhereusername',
       ssh_password='eu.pythonanywherepassword',
       remote_bind_address=('xxxx.mysql.eu.pythonanywhere-services.com', 3306),
       # debug_level='TRACE'
) as tunnel:
print("SSH tunnel established.")

try:
    conn = MySQLdb.connect(
        user='pythonanywhereusername',
        passwd='MySQLpassword',
        host = "127.0.0.1",
        port=tunnel.local_bind_port,
        db='username$default'
    )
    print("Database connection established.")
except (sshtunnel.BaseSSHTunnelForwarderError, MySQLdb.Error) as e:
    print(f"Failed to connect: {type(e).__name__}, {str(e)}")


except Exception as e:
    print(f"An unexpected error occurred: {type(e).__name__}, {str(e)}")


if __name__ == '__main__':
    app.run(debug=True)

Gives error:

SSH tunnel established.
Failed to connect: OperationalError, (2013, "Lost connection to server at 'handshake: reading initial 
communication packet', system error: 0")
2023-09-10 07:42:36,607| ERROR   | Secsh channel 0 open FAILED: open failed: Administratively prohibited
2023-09-10 07:42:36,607| ERROR   | Could not establish connection from local ('127.0.0.1', 57088) to remote 
('xxxx.mysql.eu.pythonanywhere-services.com', 3306) side of the tunnel: open new channel ssh error: 
ChannelException(1, 'Administratively prohibited')
* Serving Flask app 'squirrel_test_1'
* Debug mode: on 
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI 
server instead.
* Running on http://127.0.0.1:5000

So the Tunnel seems to work. But it goes wrrong in the "handshake"

Does anyone recognise this problem? I am happy with any help. Greetings Yves

The indentation looks a bit strange there, there should be at least some lines indented inside that "with" statement, as otherwise Python would generate a syntax error. Do you have all of the code below the "with" indented, including the "try/except" block? The tunnel will be closed as soon as you exit the "with" block.

from flask import Flask, jsonify import MySQLdb import sshtunnel

# Set SSH and Tunnel timeouts
sshtunnel.SSH_TIMEOUT = 2.0
sshtunnel.TUNNEL_TIMEOUT = 2.0

app = Flask(__name__)


# @app.route('/users', methods=['GET'])
# def get_users():
with sshtunnel.SSHTunnelForwarder(
        ('ssh.eu.pythonanywhere.com'),
        ssh_username='xxxxx',
        ssh_password='xxxxx',
        remote_bind_address=('xxxx.mysql.eu.pythonanywhere-services.com', 3306),
        # debug_level='TRACE'
) as tunnel:
    print("SSH tunnel established.")

    try:
        conn = MySQLdb.connect(
            user='xxxxx',
            passwd='xxxxxx',
            host = "127.0.0.1",
            port=tunnel.local_bind_port,
            db='xxxxx$default'
        )
        print("Database connection established.")
    except (sshtunnel.BaseSSHTunnelForwarderError, MySQLdb.Error) as e:
        print(f"Failed to connect: {type(e).__name__}, {str(e)}")


    except Exception as e:
        print(f"An unexpected error occurred: {type(e).__name__}, {str(e)}")




if __name__ == '__main__':
    app.run(debug=True)

Hi Gilles, Thanks for your resonse, i made a more clear copy of the code width clear indentations. Everything is indented after the with statement till the main block.

Is there a chance that something is going wrong in the Administratively proces?

How are you creating the tunnel? From our logs it looks like you're using "FCapi" username, instead of "FCApi" -- could you try connecting using "FCApi"? (mind the capitalization).

Dear Pafk,

I am so embarrassed, I didn't look properly. Thank you for your help anyway!!

SSH tunnel established.
Database connection established.

Glad to hear that you made it work!