1

I have flask running in a daemon on my Raspi.

@app.route("/cmd",methods = ['POST', 'GET'])
def cmd():
    if request.method == 'GET':
        order_obj = request.args.to_dict(flat=True)
    else:
        order_obj = request.get_json(force=True)
    response = jsonify(controller_obj.act_on_order(order_obj))
    response.headers.add('Access-Control-Allow-Origin', '*')
    return response

app.run(port=8087, debug=config.DEBUG, use_reloader=False)

When I run this app, I can see it is listening on port 8087:

pi@brs-tv:~/brs $ sudo netstat -lptu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name               
tcp        0      0 localhost:8087          0.0.0.0:*               LISTEN      4133/python   

When I telnet to the port locally using localhost, it works fine.

pi@brs-tv:~/brs $ telnet localhost 8087
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /cmd

But when I telnet locally to its local address, I get connection refused:

pi@brs-tv:~/brs $ telnet brs-tv.local 8087
Trying 127.0.1.1...
telnet: Unable to connect to remote host: Connection refused

Is this a Rpi thing, or a Flask thing?

Wes Modes
  • 2,024
  • 2
  • 22
  • 40

1 Answers1

1

It turns out it is a Flask thing.

host (Optional[str]) – the hostname to listen on. Set this to '0.0.0.0' to have the server available externally as well. Defaults to '127.0.0.1' or the host in the SERVER_NAME config variable if present.

So, fixing my Flask run call:

app.run(host="0.0.0.0", port=config.CONTROLLERS[whoami]["port"], 
        debug=config.DEBUG, use_reloader=False)

Now, my port is listening to the rest of the world:

pi@brs-tv:~ $ sudo netstat -lptu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:8087            0.0.0.0:*               LISTEN      1213/python 

I can also now connect from another machine:

Ricos-vt220:~ % telnet brs-tv.local 8087
Trying fe80::3d7:b64:bb26:14e0...
telnet: connect to address fe80::3d7:b64:bb26:14e0: Connection refused
Trying 192.168.86.29...
Connected to brs-tv.local.
Escape character is '^]'.
GET /cmd
Wes Modes
  • 2,024
  • 2
  • 22
  • 40