Forums

API seems to be running but always times out

Domain www.bluestreets.net. API running according to web tab, no errors in log, but always times out. I'm pretty stuck.

I'm not seeing a timeout on that domain, just a 404, so it looks like you just haven't defined a root url for your app.

Yes, I saw the 404. And defining a root URL doesn’t sound like anything I’ve done. So how do I do that?

Check the documentation for the web framework you're using. That will describe how to do it. They may refer to views or something like that.

Hmm. I should have mentioned that the API runs on my laptop and I can run it in a bash console on my pythonanywhere site. Which makes me think the problem is configuration, not internal in my Flask code.

It could be your configuration or some interaction between the configuration and your code.

Yeah. Getting strange results in the log. Root request, for example, when I certainly did not make one. Gonna start over, do one tricky bit at a time, try to isolate the problem. Stay tuned.

The root request that appears in your log is part of the start-up of your web app. Our system hits the root url to get a simple quick check that the site is ready.

OK, big clue here. This is my run file, bluestreets_api.py (I added a root route):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/python3.6

from flask import Flask
from flask_restful import Api
from config.config import configure_app, configure_api

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello from Bluestreets!'


if __name__ == '__main__':

    configure_app(app)

    api = Api(app)
    configure_api(api)

    app.run(host='0.0.0.0', port=5500)

Now, the app.route works but the flask-restful add_resource method gets a 404. Here's configure_api:

def configure_api(api):
    from resources.contacts import Contacts, ContactsSince
    from resources.precincts import Precincts
    from resources.groups import Groups
    from resources.memberships import Memberships
    from resources.users import (
        UserRegistration, Login, Refresh, Blacklist
    )

    api.add_resource(Contacts, '/con_api/all')
    api.add_resource(ContactsSince, '/con_api/since/<string:datestr>')
    api.add_resource(Precincts, '/pct_api/all')
    api.add_resource(Groups, '/grp_api/all')
    api.add_resource(Memberships, '/mem_api/all')
    api.add_resource(UserRegistration, '/usr_api/new')
    api.add_resource(Login, '/usr_api/login')
    api.add_resource(Refresh, '/usr_api/refresh')
    api.add_resource(Blacklist, '/usr_api/blacklist')

So it appears those add_resource methods are not getting routed. They work fine anywhere but on pythonanywhere.

Another thing. If I comment out the run line everything still works, or doesn't work, the same way. And putting the port # in the URL never works. Always gets "site can't be reached", even with port 8000. So what port is this running on?

configure_api is never called. The code in the __name__ if statement is only executed when the Python script is run from the command line so it is not called when that module is imported into your web app. You should also check out this

Another problem. I'm hard coding all the config stuff right into flask_app.py but it can't find module flask-praetorian. Even though I installed it using the --user flag and I see in when I pip list. Not using a virtual env on this to try to keep it simple.

Bingo! Explains everything. Works now, thanks. On to the next problem...

Cool. Glad we could help.