My code was using sshtunnel
and connected to my database correctly. However due to a conflict between grequests
and sshtunnel
I had to switch to using another library for the ssh, and have had most luck with Paramiko
(as that works withmonkeypatch
on grequests
).
The ssh
tunnel section is working correctly, however I am now getting the message:
An error occurred: 1045 (28000): Access denied for user 'suspiciousleaf'@'localhost' (using password: YES)
I'm using environment variables that haven't changed, as far as I can see everything is the same between the working code with sshtunnel and without grequests, and the version with Paramiko that can't connect.
Full code here:
from gevent import monkey
monkey.patch_all()
import grequests
import mysql.connector
import paramiko
from pprint import pprint
import os
ssh_password = os.environ.get("PA_SSH_PASS")
db_password = os.environ.get("PA_DB_PASS")
database = os.environ.get("PA_DB_NAME")
def get_current_listing_urls():
try:
ssh_client = paramiko.SSHClient()
ssh_client.load_system_host_keys()
ssh_client.set_missing_host_key_policy(paramiko.WarningPolicy)
ssh_client.connect(
"ssh.eu.pythonanywhere.com",
username="suspiciousleaf",
password=ssh_password,
)
transport = ssh_client.get_transport()
channel = transport.open_channel(
"direct-tcpip",
dest_addr=("suspiciousleaf.mysql.eu.pythonanywhere-services.com", 3306),
src_addr=("127.0.0.1", 3306),
)
db = mysql.connector.connect(
user="suspiciousleaf",
password=db_password,
host="127.0.0.1",
port=3306,
database=database,
use_pure=True,
)
table_name = "listings"
cursor = db.cursor(dictionary=True)
query = f"SELECT COUNT(*) FROM {table_name};"
cursor.execute(query)
results = cursor.fetchall()
return results
except Exception as e:
print(f"An error occurred: {str(e)}")
finally:
if "cursor" in locals() and cursor:
cursor.close()
if "db" in locals() and db:
db.close()
if "ssh_client" in locals() and ssh_client:
ssh_client.close()
old_listing_urls = get_current_listing_urls()
pprint(old_listing_urls)
I've tried just about everything I can think of, but I can't see where I'm going wrong. The message presumably means it's connecting and trying to authenticate, but failing. However the database name, username, and password are all correct as they are all working when using sshtunnel
.
Any help would be much appreciated! Thanks