Apparently, connecting to my database takes too long which throws this error. Is this likely a problem on my end or yours? I haven't seen this post before on the forums and really don't know what to try.
Apparently, connecting to my database takes too long which throws this error. Is this likely a problem on my end or yours? I haven't seen this post before on the forums and really don't know what to try.
Can you still start a mysql console from the Databases tab? If so, maybe double-check all the connection information you're giving to pymysql (hostname, password, database name, port...)
This is what my connection code looks like right now.
connection = pymysql.connect(host='sullberg31.mysql.pythonanywhere-services.com',
user='sullberg31',
password='{}',
db='sullberg31${}',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
Is there a specific port i should be passing in?
[edit by admin: formatting]
That looks OK (assuming you're replacing the {}
s with the correct values).
Are you trying to connect from code running on PythonAnywhere? Or is it running on your local machine, perhaps?
This code is running from my local machine. I was trying to test it out before using it in my web app.
Ah, that would explain it. PythonAnywhere MySQL servers aren't directly accessible from the public Internet, for security. As you have a paid account, you can access your server indirectly using an SSH tunnel -- there's documentation on how to do that here.
Thanks!
No problem! We re-wrote that help page recently, so hopefully it's all clear -- but just let us know if you have any questions or if there's anything unclear.
_
@app.route('/connect/', methods=['POST'])
def testConnect():
test = request.get_json()
sshtunnel.SSH_TIMEOUT = 5.0
sshtunnel.TUNNEL_TIMEOUT = 5.0
with sshtunnel.SSHTunnelForwarder(
('ssh.pythonanywhere.com'),
ssh_username='sullberg31', ssh_password='{login password}',
remote_bind_address=('sullberg31.mysql.pythonanywhere-services.com', 3306)
) as tunnel:
connection = mysql.connector.connect(
user='sullberg31', password='{sql password}',
host='127.0.0.1', port=tunnel.local_bind_port,
database='sullberg31${database name}',)
connection.close()
return test
I'm trying to use this code as a simple test to determine whether or not I can connect to my database (and eventually edit it) through an http post request from my local machine. However, every time I run the request I get a json decoder error which makes no sense. Is there a better way to edit my database through http requests?
Your code starts with request.get_json(). Whatever request is, the response you're getting is not json and so you're getting the error. It has nothing to do with your database.
When I move the return statement above the connection block I get the test string back no problem.
Could you post the exact stack trace?
--
Traceback (most recent call last):
File "C:/Users/Seaton/Documents/PythonProjects/Fishing/apiTest.py", line 17, in <module>
doit = requests.post(url, json=data).json()
File "C:\Users\Seaton\Anaconda3\lib\site-packages\requests\models.py", line 850, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\Seaton\Anaconda3\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\Seaton\Anaconda3\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Seaton\Anaconda3\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
[edit by admin: formatting]
OK. So what do you see if you temporarily replace the line that does the post with
print(repr(requests.post(url, json=data).text))
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>500 Internal Server Error</title>\n<h1>Internal Server Error</h1>\n<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>\n'
Problem solved! Simple as resetting my database password. Sorry for the hassle.