for loop in flask show only one url from list

Question:

please help me to find mistake and fix it in the code below.

from flask import Flask, jsonify, request
import requests
import json
import html
app = Flask(__name__)
urls = [
    'https://192.168.1.6:8080/info',
    'https://192.168.1.5:8080/info',
            
            ]
@app.route('/dev', methods=['GET'])
def req_info():
    for url in urls:
        r = requests.get(url)
        if r.status_code == 200:
            resp = json.loads(r.text)
            resp_git = resp['git']
            resp_git_commit = resp['git']['commit']
            data = ( "URL: " + f"{url}"'<br>'
                    "Branch: " + f"{(resp_git['branch'])}"'<br>'
                    "Commit: " + f"{(resp_git_commit['id'])}"'<br>'
                    "Time: " + f"{(resp_git_commit['time'])}"'<br>'
                )
    return (data)
req_info()
if __name__ == '__main__':
    app.run()

I have created a list of urls with a set of services and I am polling them with /info and taking their JSON

{
  "git": {
    "branch": "develop",
    "commit": {
      "id": "88cfv4a",
      "time": "2023-02-22T07:51:25Z"
    }
  },
  "build": {
    "artifact": "my-app",
    "time": "2023-02-22T08:09:19.528Z",
    "version": "0.0.1"
  }
}

However, why is the for loop processing and return only the last one from the list. I tried to put the return outside the loop but honestly it didn’t help or I don’t understand how to do it correctly.
I am expecting to see a list of all the urls from the list one after another

URL: https://192.168.1.6:8080/info
Branch: develop
Commit: 88cfv4a
Time: 2023-02-22T07:51:25Z

URL: https://192.168.1.5:8080/info
Branch: test
Commit: 287ca7a
Time: 2023-02-22T13:32:54Z

but I am only getting last one.
Thank you!

Asked By: kotich-io

||

Answers:

As pointed out by @jasonharper you are overriding the data variable on each iteration. The first solution that comes to my mind is to create a list where you can push each data response.

Something like this:

from flask import Flask, jsonify, request
import requests
import json
import html
app = Flask(__name__)
urls = [
    'https://192.168.1.6:8080/info',
    'https://192.168.1.5:8080/info']

@app.route('/dev', methods=['GET'])
def req_info():
    responses = []
    for url in urls:
        r = requests.get(url)
        if r.status_code == 200:
            resp = json.loads(r.text)
            resp_git = resp['git']
            resp_git_commit = resp['git']['commit']
            data = ( "URL: " + f"{url}"'<br>'
                    "Branch: " + f"{(resp_git['branch'])}"'<br>'
                    "Commit: " + f"{(resp_git_commit['id'])}"'<br>'
                    "Time: " + f"{(resp_git_commit['time'])}"'<br>'
                )
            responses.append(data)
    return (responses)
req_info()
if __name__ == '__main__':
    app.run()
Answered By: Gil Sousa
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.