Forums

Access a remote DB from a PythonAnywhere Python script or connect to a PythonAnywhere DB from remote website

Hi -

Is it possible to connect to a remote DB from a PythonAnywhere Python script?

If not, is it possible to connect to a PythonAnywhere DB from a remote website?

If the answer to both of those questions is "no", can someone offer some alternative solutions?

Any advice would be greatly appreciated.

  1. Yes- as a paying user, you can make any outbound connections from PythonAnywhere. (so you can connect to a remote DB)
  2. Yes- but it will involve ssh tunnelling to keep your DB safe from the internet. (so an external script can connect to your PythonAnywhere DB, but only if you let them ssh into your account- ie. anyone with access to that script will have full access to your account)

Thanks for the reply.

That's great news about being able to connect to a remote DB!!

When I tried to do so, it came back with following error:

_mysql_exceptions.OperationalError: (1045, "Access denied for user 'mydbuser'@'longhostname.com' (using password: YES)")

And I think I know why this is happening... apparently my hosting provider only allows incoming database connections from whitelisted IP addresses.

Is there a way I can find out the IP address that my Python script is using so I can add it to my hosting account's whitellist?

you can use the ipify api to find out. but keep in mind that our IP addresses may change at any time and we will not notify you.

Thanks for pointing me in the direction of Ipify - what a nifty tool!

I did some testing, and it seems that PA uses some sort of load balancing because the first two octets of the IP addresses returned from Ipify alternated.

In the event that PA's IP addresses change, will those first two octets remain constant? Or is it possible that all octets could change in the future, without notice?

My hosting provider allows 'wildcards', so I could add 123.456.%.% to the whitelist if I knew that the first two octets would remain constant, for example.

Hi there, I'm afraid we can't guarantee anything at all about our IP addresses, no. Are you running this script from a webapp, a console, or a somewhere else?

Hi Harry, the script is running as a scheduled task. Thanks.

Yes, then I'm afraid we really have no way to provide any kind of stable IP address. For what it's worth, they usually only change when we have scheduled downtime (which we email you about), and those only happen every 4-6 weeks, but we can't suggest that you rely on that because there are times when the IP address might change outside of those scheduled slots...

Sorry I can't be more helpful!

Thanks for the info.

I've just been using a %.%.%.% wildcard, which allows any IP address to reach my remote database, and it's been working fine. And since my application isn't being used for anything 'mission critical', I'm fine with it being open like that.

Thanks to you and Conrad for the help. My script is my first venture into Python, and it's been a great ride so far, made even greater by PythonAnywhere. Keep up the great work!! :)

hooray! Glad it's working :)