Getting socket.gaierror: [Errno 11001] getaddrinfo failed Error

17120
1
07-21-2021 03:26 AM
CliveSwan
Occasional Contributor II

Greetings,

I am getting a socket.gaierror Error When running a script to read the Portal Services.

The code fails at: 

httpConn.request("POST", folderURL, params, headers)

The code is:

 

    # Construct URL to read folder
    if str.upper(folder) == "ROOT":
        folder = ""
    else:
        folder += "/"

    folderURL = "/arcgis/admin/services/" + folder

    #### This request only needs the token and the response formatting parameter
    params = urllib.parse.urlencode({'token': Token, 'f': 'json'})
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}

    #### Connect to URL and post parameters
    httpConn = http.client.HTTPSConnection(serverName, serverPort)
    httpConn.request("POST", folderURL, params, headers)

    #### Read response
    response = httpConn.getresponse()
    if (response.status != 200):
        httpConn.close()
        print
        "Could not read folder information."
        return
    else:
        data = response.read()

        # Check that data returned is not an error object
        if not assertJsonSuccess(data):
            print
            "Error when reading folder information. " + str(data)
        else:
            print
            "Processed folder information successfully. Now processing services..."

        #### Deserialize response into Python object
        dataObj = json.loads(data)
        httpConn.close()

        #### Loop through each service in the folder and stop or start it
        for item in dataObj['services']:
            fullSvcName = item['serviceName'] + "." + item['type']
            #### Construct URL to stop or start service, then make the request
            statusURL = "/arcgis/admin/services/" + folder + fullSvcName + "/status"
            httpConn.request("POST", statusURL, params, headers)

            #### Read status response
            statusResponse = httpConn.getresponse()
            if (statusResponse.status != 200):
                httpConn.close()
                print
                "Error while checking status for " + fullSvcName
                return
            else:
                statusData = statusResponse.read()

                #### Check that data returned is not an error object
                if not assertJsonSuccess(statusData):
                    print
                    "Error returned when retrieving status information for " + fullSvcName + "."
                    print
                    str(statusData)

                else:
                    #### Add the stopped service and the current time to a list
                    statusDataObj = json.loads(statusData)
                    if statusDataObj['realTimeState'] == "STOPPED":
                        stoppedList.append([fullSvcName, str(datetime.datetime.now())])

            httpConn.close()

            #### Check number of stopped services found
    if len(stoppedList) == 0:
        print
        "No stopped services detected in folder " + folder.rstrip("/")
    else:
        #### Write out all the stopped services found
        #### This could alternatively be written to an e-mail or a log file
        for item in stoppedList:
            print
            "Service " + item[0] + " was detected to be stopped at " + item[1]

    return

#### A function that checks that the input JSON object is not an error object.
def assertJsonSuccess(data):
    obj = json.loads(data)
    if 'status' in obj and obj['status'] == "error":
        print
        "Error: JSON object returns an error. " + str(obj)
        return False
    else:
        return True


#### Script start
if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))

 

 

The error message points to a path error??

 

Traceback (most recent call last):
  File "C:/Users/tmpgcesn/PycharmProjects/TEST/venv/InternalProd_ListRestStatus.py", line 144, in <module>
    sys.exit(main(sys.argv[1:]))
  File "C:/Users/tmpgcesn/PycharmProjects/TEST/venv/InternalProd_ListRestStatus.py", line 62, in main
    httpConn.request("POST", folderURL, params, headers)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\http\client.py", line 1277, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\http\client.py", line 1323, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\http\client.py", line 1272, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\http\client.py", line 1032, in _send_output
    self.send(msg)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\http\client.py", line 972, in send
    self.connect()
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\http\client.py", line 1439, in connect
    super().connect()
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\http\client.py", line 944, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\socket.py", line 752, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

Process finished with exit code 1

 

 

 

0 Kudos
1 Reply
arahman_mdmajid
Occasional Contributor

Have a look at the script I posted here for listing out all the services based on the python request module instead of using the httplib module.

Abdur Rahman
GIS Developer
0 Kudos