0

I'm writing a flask application, and within the application is a Dash dashboard, and inside one of its callbacks I'm attempting to access an API endpoint developed in the larger flask project.

The API endpoint works correctly when tested in the browser, but if I run: requests.get(url="http://192.168.X.X:5000/api/v1/search", params=params)

I get the following error:

HTTPConnectionPool(host='192.168.X.X', port=5000): Max retries exceeded with url: /api/v1/search (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001A17D8DBB50>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

This is the function I'm using to deliver the API results:

@bp.route('/v1/search', methods=['GET'])
def search_prediction():
    info_dict = dict(
        city = request.args['city'],
        subject_age = int(request.args['subject_age']),
        subject_race = request.args['subject_race'],
        subject_sex = request.args['subject_sex'],
        reason_for_stop = request.args['reason_for_stop'],
        hour = int(request.args['hour']),
        dayofweek = int(request.args['dayofweek']),
        quarter = int(request.args['quarter'])
    )
    
    sample = pd.DataFrame(info_dict, index=[0])
    info_dict['proba'] = float(stop_search_mod.predict_proba(sample)[0][1])
    info_dict['outcome'] = 'search'
   
    
    return jsonify(info_dict)

My config.py file looks like this:

from os import environ, path

from dotenv import load_dotenv

BASE_DIR = path.abspath(path.dirname(__file__))
load_dotenv(path.join(BASE_DIR, ".env"))


class Config:
    """Flask configuration variables."""

    # General Config
    FLASK_APP = environ.get("FLASK_APP")
    FLASK_ENV = environ.get("FLASK_ENV")
    SECRET_KEY = environ.get("SECRET_KEY")

    # Static Assets
    STATIC_FOLDER = "static"
    TEMPLATES_FOLDER = "templates"
    COMPRESSOR_DEBUG = environ.get("COMPRESSOR_DEBUG")

When I ping the URL in my browser with parameters arguments added in I get the correct result:

enter image description here

2
  • are requests too frequent? if so need to put some sleep in between the calls. another option would be with retry. something like this import requests from requests.adapters import HTTPAdapter sess = requests.Session() sess.mount('http://', HTTPAdapter(max_retries=5)) res=sess.get("http://someendpoint/") Commented May 17, 2021 at 16:44
  • I am only making one request at a time. This is just done in testing and I call the function only once. Commented May 17, 2021 at 18:45

1 Answer 1

1

Try

```requests.get(url="http://localhost:5000/api/v1/search", params=params)```

i.e replace the ip address with 'localhost'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.