Trying to use API to call specific data from SQL table

I have been trying to get a section of code to work and can get 1 way to search by the lane column, but the api wants to call by Origin City and Destination fields in the mssql database.

This is returned in the when it is searched in the browser with https://IPaddr:5000/rates/city?origin_city=FARGO&destination=ND

Browser:
{
“error”: “HTTPConnectionPool(host=‘127.0.0.1’, port=5000): Max retries exceeded with url: /rates/city?origin_city=FARGO&destination=ND (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x000002821152BA50>: Failed to establish a new connection: [WinError 10055] An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full’))”
}

Python App
{
127.0.0.1 - - [17/Oct/2023 14:11:50] “GET /rates/city?origin_city=FARGO&destination=ND HTTP/1.1” 200 -
Request URL: http://127.0.0.1:5000/rates/city?origin_city=FARGO&destination=ND
Response status code: 200
Response content: {
“error”: “HTTPConnectionPool(host=‘127.0.0.1’, port=5000): Max retries exceeded with url: /rates/city?origin_city=FARGO&destination=ND (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x000001A70127DD50>: Failed to establish a new connection: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted’))”

}

My Routes.py looks like this:

from flask import Flask, jsonify, request
import requests
from models import Rates  # Import your model

BASE_URL = 'http://127.0.0.1:5000'

def get_rates_by_city():
    origin_city = request.args.get('origin_city')
    destination = request.args.get('destination')

    try:
        response = requests.get(f'{BASE_URL}/rates/city', params={'origin_city': origin_city, 'destination': destination})
        
        # Log the URL and response
        print('Request URL:', response.url)
        print('Response status code:', response.status_code)
        print('Response content:', response.text)
        
        # Return the JSON response
        return response.json()

    except requests.exceptions.RequestException as e:
        return jsonify({'error': str(e)}), 500

def setup_routes(app):
    @app.route('/rates', methods=['GET'])
    def get_rates():
        try:
            rates = Rates.query.all()
            rates_data = [{'lane': rate.lane, 'origin_city': rate.origin_city, 'destination': rate.destination, 'min_rate': rate.min_rate} for rate in rates]
            return jsonify(rates_data)
        except Exception as e:
            return jsonify({'error': str(e)}), 500

    @app.route('/rates/city', methods=['GET'])
    def handle_get_rates_by_city():
        return get_rates_by_city()

    @app.route('/rates/lane/<lane>', methods=['GET'])
    def get_rates_by_lane(lane):
        try:
            rates = Rates.query.filter_by(lane=lane).all()
            rates_data = [{'lane': rate.lane, 'origin_city': rate.origin_city, 'destination': rate.destination, 'min_rate': rate.min_rate, 'per_mile_rate': rate.per_mile_rate} for rate in rates]
            return jsonify(rates_data)
        except Exception as e:
            return jsonify({'error': str(e)}), 500

Hey Tonny from this it seems another program is running on this… have you tried checking if another program is using the same port or try using another port number…

I should have clarified I check ‘netstat -an’ when the app isn’t running and there is nothing on 5000. I then run the ‘python app.py’ in powershell and it starts on 5000, but then I get that response when running a query.

What really throws me is that it returns a 200 in the request and then I get that error displayed.

I will also add that when this is queried, it repeats the same error to fill the entire buffer. One interesting note is that the first query come from the ip address of the original query, but the subsequent all show the internal IP.

continuing testing and watching in visual studio code, it opens 15000 threads and then continues to crash with the error:

10.0.0.84 - - [17/Oct/2023 22:32:40] “GET /rates/city?origin_city=FARGO&destination=ND HTTP/1.1” 200 -
Request URL: http://10.0.0.84:5000/rates/city?origin_city=FARGO&destination=ND
Response status code: 200
Response content: {
“error”: “HTTPConnectionPool(host=‘10.0.0.84’, port=5000): Max retries exceeded with url: /rates/city?origin_city=FARGO&destination=ND (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x0000022EA98CF2D0>: Failed to establish a new connection: [WinError 10055] An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full’))”
}

maybe I am going about this all wrong, but I am open to ideas on what I can do to get this working.

Aha ummh so, can you reduce the number of concurrent requests or the number of threads.
after looking at the view again lets make a session based request to check the buffer size
so semthing like

session = requests.Session()

def get_rates_by_ciry():
    # ori---
    #  destination =
    try:
       response = session.get(.....

Ok, I was able to get this responding to internal requests as expected, but it seems to be a different result when trying to access it from outside of the network. Any idea why they would respond differently?

When calling, I just get the to square brackets as the response. again the python script shows 200 as the result but nothing is displayed.

Hey, I have no idea why it’s behaving that way seems strange, if the status code is okay then the content you want displayed should show up…but ummh @Rosuav might have an idea

Wait! that was my fault. I need to spell origin right in my URL. Thank you guys! Everything is working as expected now!

it worked after making a persisting connection or the spelling has been the issue since beginning?

I realized a had a weird call to my get rates by City function, then realized the second part of not working outside the network was a silly spelling mistake

Ah nice that’s what’s up…