Forums

Python Anywhere API Not Working: Reload Webapp Returning Server Error

The code below is the PA generated script when I created an API token (minus the actual token):

import requests
my_domain = 'roasted.pythonanywhere.com'
username = 'roasted'
token = 'token'
response = requests.post(
'https://www.pythonanywhere.com/api/v0/user/{username}/webapps/{domain}/reload/'.format(
username=username, domain=my_domain),
headers={'Authorization': 'Token {token}'.format(token=token)})
if response.status_code == 200:
  print('All OK')
else:
  print('Got unexpected status code {}: {!r}'.format(response.status_code, response.content))

I tested a few of the other API calls and they work fine.

I need to reload my webapp at the end of a scheduled task or the newly created json file won't be loaded on my site.

Here's the error message:

Got unexpected status code 500: b'<html>\r\n<head><title>500 Internal Server Error</title></head>\r\n<body bgcolor="white">\r\n<center><h1>500 Internal Server Error</h1></center>\r\n<hr><center>openresty/1.9.15.1</center>\r\n</body>\r\n</html>\r\n'

Hi there, thanks for reporting- do you mind us taking a look at your code?

We would like to try to repro it on our side.

Yes, that's no problem.

oops, also can you give the path to the file in question?

https://www.pythonanywhere.com/user/roasted/files/home/roasted/croasted/scripts/scheduled.py

or home/roasted/croasted/scripts/scheduled.py

More testing has revealed that if I manually change the file and then click the save button, the changes are reflected on the site. That is, the correct file is loading... even without doing an app reload.

If I can invoke the code that performs the save at the end of my scheduled.py script I don't need to reload the app with an API call.

If that's impossible I might be able to automate a web-browser and with selenium just click the button (for that matter, either the save or reload)... although that is quite a hacky solution (but it's not beneath me).

The save button does not do anything special. It just writes to the file so, if it updates because of a save, it should update because you wrote a new file.

You're getting the error, though, because you're reloading a web app that does not exist. Use the domain name for the web app you want to reload.

Ah, you guys made some lazy code that doesn't have a try/except or if/else check on the name of a user's app when you auto-generate the script for them to test a reload.

No worries. If someone else has this problem none of these worked for me:

  1. domain_name.com
  2. https://www.domain_name.com
  3. https://domain_name.com
  4. etc, etc

The version that worked for me: www.domain_name.com

I can't verify that the code they generate for you will work. Obviously, in my case it didn't.

As for the other question, if you guys are willing to bet me $100 or more of account credit I'll prove it remotely for you. Let me know if you will put your money where your mouth is ;-)

ps: I'm still pretty new to programming, so I'm getting used to the level of snark that most programmers seem to adhere to. If I missed the mark... fuck you.

Cheers =)

That's not an auto-generated script. It's an example.

I was stuck on this until I applied the above fix, namely: www.domain_name.com

Glad to hear that you made it work!