Forums

Bottle web app works in terminal but errors as webapp with 502-backend

Hello. I'm trying to run a python web page but it doesn't load from the webapp url. Instead it shows "Something went wrong :-( Error code: 502-backend" However, when i run the wsgi from the bash terminal it loads without any problems on localhost.

This is the wsgi.py code that tries to import the main page:

import bottle
import os
import sys

project_home = '/home/RIZ/Project'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

templates_dir = os.path.join(project_home, 'views/')
if templates_dir not in bottle.TEMPLATE_PATH:
bottle.TEMPLATE_PATH.insert(0, templates_dir)

from backend import app
application = app

And this is a snippet from code of the main app I'm trying to load:

from bottle import default_app
from waitress import serve

if __name__ != '__main__': #because == was never true for some reason
    app = default_app()
    serve(app, listen='127.0.0.1:8000')

Furthermore, there are no errors in error log but the server log contains this before it ends:

2021-09-10 19:06:02 Config file not found.
2021-09-10 19:06:02 VACUUM: unix socket /var/sockets/riz.pythonanywhere.com/socket removed.

The line if __name__ == '__main__' will always be false on PythonAnywhere, because that is only true when you run the file directly to use the development server, which PythonAnywhere does not do. It needs to be == so that that code does not get run.

If you correct the line so that it has ==, as it should and move the app = line out of the if block, then your site should work.

Thank you for your reply. Unfortunately this doesn't seem to work. I've already tried many different combinations of the if block but it just never works.

Use this:

app = default_app()
if __name__ == '__main__': 
    serve(app, listen='127.0.0.1:8000')