Forums

sshtunnel.BaseSSHTunnelForwarderError: Could not establish session to SSH gateway

Hi,

I wrote the following code, but I get this error when I run it:

sshtunnel.BaseSSHTunnelForwarderError: Could not establish session to SSH gateway

Any idea on what could be the issue? I'm using python 3.8 and I have a paid subscription on the .com site (not EU).

Thanks!


import mysql.connector
import sshtunnel
import pandas as pd

sshtunnel.SSH_TIMEOUT = 5.0
sshtunnel.TUNNEL_TIMEOUT = 5.0

with sshtunnel.SSHTunnelForwarder(
    ('ssh.pythonanywhere.com',22),
    ssh_username='myuser', ssh_password='mypass',
    remote_bind_address=('my_user.mysql.pythonanywhere-services.com',3306)
    ) as tunnel:
        connection = mysql.connector.connect(user='myuser', password='mypass',host='127.0.0.1', port=tunnel.local_bind_port,database='myuser$mydb')
        sql = 'SELECT * from history'
        df = pd.read_sql(sql, con=connection)
        print(df.head())
        connection.close()

I see a lot of failed password attempts just before this post was sent and then many more successful SSH connections, so I'm guessing that you started using the correct password and that it's working now.

you got it right...I had the wrong password in the connector. Now it's working properly.

Thanks!

Great! Glad you got it working.

Hey there!

I don't used ssh before and now I need a help !

server = sshtunnel.SSHTunnelForwarder( ('ssh_host',22), ssh_username='myuser', ssh_password='mypass', remote_bind_address=('my_user.mysql.pythonanywhere-services.com',3306) )

i don't know what to put in ssh_host :(

help me

Hi, ssh only works for paid accounts.

Good day.

I am having similar issue right now.

sshtunnel.BaseSSHTunnelForwarderError: Could not establish session to SSH gateway.

My question is, where do I paste the code. I pasted the code in my settings.py file, I am not sure if that is the right place

import psycopg2
import sshtunnel

sshtunnel.SSH_TIMEOUT = 5.0
sshtunnel.TUNNEL_TIMEOUT = 5.0

postgres_hostname = "yourusername-1234.postgres.pythonanywhere-services.com"  # You will have your own here
postgres_host_port = 1234  #  You will have your own port here

with sshtunnel.SSHTunnelForwarder(
        ('ssh.pythonanywhere.com'),
        ssh_username='your PythonAnywhere username',
        ssh_password='the password you use to log in to the PythonAnywhere website',
        remote_bind_address=(postgres_hostname, postgres_host_port)
) as tunnel:
    connection = psycopg2.connect(
        user='a postgres user', password='password for the postgres user',
        host='127.0.0.1', port=tunnel.local_bind_port,
        database='your database name',
    )
    # Do stuff inside the context manager block
    connection.close()

Thank you.

[edit by admin: formatting]

Is this for running a Django website on your own machine, but having it communicate with a Postgres server hosted on PythonAnywhere? Or are you doing something else?

It is for running a Django website on my own machine development environment which is Visual Studio Code, but having it communicate with a Postgres server hosted on PythonAnywhere.

Thanks

Good day sir. Waiting for response to my request sir.

Okay, I see. At the point in the code "# Do stuff inside the context manager block" you have a connection which you can call connection.cursor() on to be able to perform queries. Does that answer your question, it wasnt super clear?

The question is, how can my application in visual studio code, on my laptop, connect to my postgresql database instance on Pythonanywhere server.

I have an instance of database on my laptop, but no longer sufficient for what I'm doing. I want to troubleshoot using my life data on Pythonanywhere.

I tried with the code I described above but throwing errors.

Thanks.

For debugging I dont think I'd recommend connecting your development environment to your live database. You could end up corrupting live data, schema etc. Maybe you could think about taking a dump of the live database and restoring it locally?

Thank you for this update. I will look up how to do a dump of database in PythonAnywhere.

Thank you very much, I got it working. I appreciate the support.

Great, thanks for letting us know!