'Failed to establish a new connection: [WinError 10060] A connection attempt failed

I created a simple python app that has a client part that accesses (uses the requests library) another app (api app) using Flask library to get data. Everything works when I run it locally.I run the api app and then tried running the client in my post request to get data format json from file format xml :

response = requests.post("https://appapi90.azurewebsites.net:5050/files/result.xml")

The error I am getting is:

requests.exceptions.ProxyError: HTTPSConnectionPool(host='appapi90.azurewebsites.net', port=5050): Max retries exceeded with url: /files/result.xml (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000227088081F0>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond')))

and this is my code

import requests
import os

UPLOAD_DIRECTORY = "upload"
API_URL = 'https://appapi90.azurewebsites.net:5050'

if not os.path.exists(UPLOAD_DIRECTORY):
    os.makedirs(UPLOAD_DIRECTORY)

with open('result.xml') as file:
    content = file.read()

response = requests.post('{}/files/result.xml'.format(API_URL), data=content )

def download_url(url):
    #print("downloading: ", url)
    # assumes that the last segment after the / represents the file name
    # if url is abc/xyz/file.txt, the file name will be file.txt
    file_name_start_pos = url.rfind("/") + 1
    file_name = url[file_name_start_pos:]
    pathfile = os.path.join(UPLOAD_DIRECTORY, file_name)
    r = requests.get(url, stream=True)
    if r.status_code == requests.codes.ok:
        with open(pathfile, 'wb') as f:
            for data in r:
                f.write(data)

# download a sngle url
# the file name at the end is used as the local file name
download_url("{}/data.json".format(API_URL))
download_url("{}/data.csv".format(API_URL))


# print json content
print(response)


Solution 1:[1]

  • If you are using FastAPI/uvicorn Python web app then use the below startup command: gunicorn -w 4 -k uvicorn.workers.UvicornWorker application:app

 

  • If you are using Flask Python web app then use the below startup command: gunicorn --bind=0.0.0.0 --timeout 600 application:app

  You tried and confirmed that with the above flask startup command it fixed your issue.

  Since we have automatic port detection. Only Azure web app for containers you can specify an app setting called WEBSITES_PORT and give it the value of the expected port number. This is only used to EXPOSE the Port internally to your docker container but publicly to expose port is not possible and it will be 80/443 only. The Azure App Service is PaaS offering and opening of other ports are not allowed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ossama assaghir