Search code examples
pythonamazon-web-servicesflaskpostmanamazon-ecs

Can't send requests from py script to flask on ECS but from postman it works


I have Flask on top of ECS, trying to send req (from local script) with Python requests \ http.client but it does not get to the ECS (don't see it in logs), but when trying with Postman (web and locally) it succeeds.

Any ideas why?

Flask:

app = flask.Flask(__name__)

@app.route("/infr", methods=["POST"])
def transformation():
 doing something

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)

Python script 1

def exec_task(task, out):
        import http.client
        import json

        url = "<ecs-public-ip>"
        port = 8080
        endpoint = "/infr"

        conn = http.client.HTTPConnection(url, port)
        headers = {"Content-Type": "application/json", "Connection": "keep-alive"}

        data = {
            "task": {
                "bucket": task.bucket,
                "prefix": task.prefix,
                "metadata": task.metadata
            }
        }

        payload = json.dumps(data)

        conn.request("POST", endpoint, body=payload, headers=headers)
        response = conn.getresponse()

        if response.status == 200:
            print("\nRequest successful")
            response_data = json.loads(response.read().decode())
            out.put(ProcessedTask(bucket=task.bucket, prefix=task.prefix, metadata=response_data))
        else:
            print("\nRequest failed")

        conn.close()

Python script 2

def exec_task(task, out):
        import requests

        url = "http://<ecs-public-ip>:8080/infr"
        data = {"task": {
            "bucket": task.bucket,
            "prefix": task.prefix,
            "metadata": task.metadata
        }}
        
        print("sending: " + str(data))
        headers = {"Content-Type": "application/json", "Connection": "keep-alive"}
        response = requests.post(url, json=data, headers=headers)

        if response.status_code == 200:
            print("\nRequest successful")
            response_data = response.json()
            out.put({ProcessedTask(bucket=task.bucket, prefix=task.prefix, metadata=response_data)})
        else:
            print("\nRequest failed")

Postman

enter image description here

  • Note: Port 8080 is used. Removed by mistake

Solution

  • found the problem. for some reason an exception was raised and stdout nor vscode debug did not catch it so it just ended the program.