Forums

connect Flask API to ngrok

I am using the following code trying to establish an always on API:

from flask import Flask, jsonify
import json
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
import time

application = Flask(__name__)


def load_data():
    global data
    with open('output.json', 'r') as json_file:
        data = json.load(json_file)

    # Get the current time in seconds since the epoch
    timestamp = time.time()
    # Convert the timestamp into a human-readable format
    readable_time = datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
    data['api_refreshed_time'] = str(readable_time)
    print(f"Data has been refreshed {readable_time}")


load_data()

scheduler = BackgroundScheduler()
scheduler.add_job(func=load_data, trigger="interval", seconds=20)
scheduler.start()


@application.route("/", methods=['GET'])
# @application.route('/get_data', methods=['GET'])
def get_data():
    return jsonify(data)

# application.run(port=80)

and in the bash I am using

./ngrok config [my tocken]
./ngrok http 80

I am getting

ERR_NGROK_8012
Traffic was successfully tunneled to the ngrok agent, but the agent failed to establish a connection to the upstream web service at localhost:80. The error encountered was:

dial tcp 127.0.0.1:80: connect: connection refused

I am a paied user of python anywhere... but the backend give me

Session Status                online                                                                                                                                                                                                                                           
Account                       zt (Plan: Free)                                                                                                                                                                                                                                  
Version                       3.3.4                                                                                                                                                                                                                                            
Region                        United States (us)                                                                                                                                                                                                                               
Latency                       10ms                                                                                                                                                                                                                                             
Web Interface                 http://127.0.0.1:4040                                                                                                                                                                                                                            
Forwarding                    https://d6e5-54-160-240-88.ngrok-free.app -> http://localhost:80

Connections                   ttl     opn     rt1     rt5     p50     p90                                                                                                                                                                                                      
                              4       0       0.03    0.01    0.00    0.00                                                                                                                                                                                                     
Console closed.                                                                                                                                                                                                                                                                
HTTP Requests                                                                                                                                                                                                                                                                  
-------------

GET /favicon.ico               502 Bad Gateway                                                                                                                                                                                                                                 
GET /                          502 Bad Gateway                                                                                                                                                                                                                                 
GET /favicon.ico               502 Bad Gateway                                                                                                                                                                                                                                 
GET /                          502 Bad Gateway

Is it I shouldn't use port 80? the other ports I was tring does not work either? Any suggestions of fixing it?

Thank a lot!

it seems to be port problem? When I forward 4040 port (interface), I can access over the web. But not anyother port? Which port should I use?

If you want to deploy your web app on PythonAnywhere, you need to set it up on the Web page, see this help page for more details. The way you try to do this with ngrok will not work.