我是 Python 和 mySQL 的新手,我正在尝试让以下 Python 脚本在托管网络服务器上作为 CGI 运行:
#!/usr/bin/python3
import cgi
import sys
sys.path.append("PATH TO SIDE PACKAGES DUE TO MISSING ROOT ACCESS")
import mysql.connector
from mysql.connector import Error
print('Content-type: text/html') # the mime-type header.
print() # header must be separated from body by 1 empty line.
try:
connection_config_dict = {
'user': 'MY_USER',
'password': 'MY_PASSWORD',
'host': 'rdbms.strato.de',
'database': 'MY_DATABASE',
}
connection = mysql.connector.connect(**connection_config_dict)
if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = connection.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("Your connected to database: ", record)
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
不幸的是,这会导致以下错误消息:
Error while connecting to MySQL 2055: Lost connection to MySQL server at 'rdbms.strato.de:3306',
system error: 104 Connection reset by peer
我用错误消息查找了较旧的线程,虽然我可以分别找到它们,但我没有在这个组合中找到它们,因此我希望在我处理我的提供商糟糕的客户服务之前,可能有人对解决方案有想法再次。对我这个新手来说,似乎服务器拒绝了连接,即使我来自网络内部(在服务器上作为 CGI 运行)。
有关环境的更多信息:Python 版本为 3.8.1,mySQL 服务器版本为 5.7,mysql_connector_python 版本为 8.0.23。由于它是一个托管的网络服务器,我没有 root 访问权限,因此我通过 ssh 使用 pip --user 安装了其他模块,并添加了脚本的路径,该路径可以与其他模块(如请求)一起正常工作。我也确信 mySQL 登录数据是正确的,因为它可以完美地与 PHP 脚本配合使用。
提前感谢您的帮助!