3

I am trying to run a Flask App locally and running into connection to localhost refused issues. My app directory structure looks something like this:

Directory

- index.py
- app.py
auth
-- init.py 

Contents of `init.py` 

from flask import Flask,redirect
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug.serving import run_simple
from index import application as dashApp

@server_auth.route('/dashboard')
@login_required
def dashboard():
    return redirect('/dashboard')

app = DispatcherMiddleware(server_auth,
                           {'/dashboard': dashApp.server})

# Change to port 80 to match the instance for AWS EB Environment
if __name__ == '__main__':
    run_simple('0.0.0.0', 80, app, use_reloader=True, use_debugger=True)

I launch the App using gunicorn auth:app command.

[2022-02-11 20:57:24 -0800] [2273] [INFO] Starting gunicorn 20.1.0
[2022-02-11 20:57:24 -0800] [2273] [INFO] Listening at: http://127.0.0.1:8000 (2273)
[2022-02-11 20:57:24 -0800] [2273] [INFO] Using worker: sync
[2022-02-11 20:57:24 -0800] [2274] [INFO] Booting worker with pid: 2274

I have tried a few things to troubleshoot the issue.

netstat -avn | grep 8000
tcp4       0      0  127.0.0.1.8000         *.*                    LISTEN      131072 131072   2273      0 0x0100 0x00000006

Turned off the firewall, flushed dns cache, clear browser cache as mentioned in this link:

https://www.hostinger.com/tutorials/localhost-refused-to-connect-error

kms
  • 1,810
  • 1
  • 41
  • 92
  • A couple of questions to try to help: where is your problem? Locally or in Elastic Beanstalk? Which SO are you using? – aaossa Feb 23 '22 at 14:21

4 Answers4

2

with limited info given, I suggest you can try a few other things:

  1. create a simple / get API(without any decorator) and try to hit it with breakpoint() in code to check.
  2. Instead of gunicorn , first try to run with a normal server
python init.py
0

If the other answer by @Vismay doesn't work try changing your port from 80 to 8080. Since you didn't provide minimum info, Port 80 might be in use by other services

takudzw_M
  • 186
  • 1
  • 5
0

Could you try run lsof -i:80 and lsof -i:8000 to determine if they are used.

Chino Chang
  • 178
  • 6
0

Maybe your problem is related to "# Change to port 80 to match the instance for AWS EB Environment". Assuming that you're trying to run it locally, consider that in Linux "You need root to run at port 80" (i.e. use sudo to run your server) while in Windows usually ports under 1024 are considered privileged ports and might need a similar solution. How to fix? Try using a different port for your Flask app (say, 5000).

In other case, it seems that Elastic Beanstalk only uses port 80 for inbound traffic (connection with other instances), so maybe you should try to use a different port. For example, in this part of the EB docs they just use port 8000. This seems to be the same conclusion reached in another SO question: Running Flask port 80 on Elastic-Beanstalk Worker

The ELB worker is connected to an SQS queue, by a daemon that listens to that queue, and (internally) posts any messages to http://localhost:80. Apache is listening on port 80. (...) So the solution I found, is to change which port the local daemon posts to - by reconfiguring it via a YAML config-file, it will post to port 5001, where my Flask app was running. This mean Apache can continue to handle the health-checks on port 80, and Flask can handle the SQS messages from the daemon.

aaossa
  • 3,763
  • 2
  • 21
  • 34
  • I have had it set to `80` and successfully launch the flask app locally. I am on macOS for my local setup. – kms Feb 23 '22 at 18:45
  • So the problem is making it work in your remote server? – aaossa Feb 23 '22 at 18:48
  • I am not able to launch the app locally on `http://127.0.0.1:8000` with `gunicorn` command, I get `[2022-02-24 03:37:12 +0900] [7514] [INFO] Starting gunicorn 20.1.0 [2022-02-24 03:37:12 +0900] [7514] [INFO] Listening at: http://127.0.0.1:8000 (7514)`, but it fails to load the app in the browser. – kms Feb 23 '22 at 18:50
  • Does it work running the script normally (using `port=8000` in your `*.py` file)? Or does the problem appear only using gunicorn? – aaossa Feb 23 '22 at 19:11
  • I tried to launch using `flask run` and I get: `export FLASK_APP=auth:app (base) $ export FLASK_ENV=development (base) $ flask run * Serving Flask app "auth:app" (lazy loading) * Environment: development * Debug mode: on * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit) ` – kms Feb 23 '22 at 19:22
  • I see a `flask exception: flask.cli.NoAppException flask.cli.NoAppException: A valid Flask application was not obtained from "auth:app".` – kms Feb 23 '22 at 19:23
  • I don't get any errors when I run, `python init.py`. – kms Feb 23 '22 at 19:28
  • Does this issue help? It seems like you're not passing the right object to your `MiddlewareDispatcher`: [Should use `DispatcherMiddleware(app.wsgi_app, ...)` instead](https://github.com/pallets/flask/issues/3256#issuecomment-500214283) – aaossa Feb 23 '22 at 19:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242340/discussion-between-kms-and-aaossa). – kms Feb 23 '22 at 19:39