Hi,
I am trying to create a list of all services that we have in our Portal. I have seen this script in ArxcGIS Server Administration (https://enterprise.arcgis.com/en/server/latest/administer/windows/example-write-properties-of-all-services-to-a-csv-file.htm).
When I run it using our server name, port, and my credentials as administrator, I get this error message:
gaierror: [Errno 11001] getaddrinfo failed
I am using IDLE but do I need to use Python 3 here? We are still using 10.6. Thanks.
Hi Dionisiobattad,
The linked to the scripts are written in python 2.x - the print statements do not have parentheses.
The error message you have received is likely a python urllib or https error. Your machine may not have access to the requested url that you sent. Some possible reasons may be that you have to use a proxy to access your url or simply, the request is incorrect.
Also check that the getToken() returns a token. Try running that separately. the admin/generateToken only works on ArcGIS Server if GET is enabled for tokens which it is often not for security reasons.
Hope this information helps.
You wrote "Also check that the getToken() returns a token. Try running that separately. the admin/generateToken only works on ArcGIS Server if GET is enabled for tokens which it is often not for security reasons."
Can you please elaborate in more detail how to perform the getToken troubleshooting you have referred to here?
The function getToken() (near the bottom) can be called by itself. Check that you are receiving a token from ArcGIS Server given your input arguments.
You can create a new script which contains only the getToken() function code block and execute with your desired python environment or paste the function into your python interpreter.
Something like the below should work given your input arguments.
If you are looking at Portal items and urls specifically, I would suggest looking into the arcgis.gis module which searches on Portal rather than the ArcGIS Server REST Admin API.
def getToken(username, password, serverName, serverPort): # Token URL is typically http://server[:port]/arcgis/admin/generateToken tokenURL = "/arcgis/admin/generateToken" params = urllib.urlencode({'username': username, 'password': password, 'client': 'requestip', 'f': 'json'}) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} # Connect to URL and post parameters httpConn = httplib.HTTPConnection(serverName, serverPort) httpConn.request("POST", tokenURL, params, headers) # Read response response = httpConn.getresponse() if (response.status != 200): httpConn.close() print "Error while fetching tokens from admin URL. Please check the URL and try again." return else: data = response.read() httpConn.close() # Check that data returned is not an error object if not assertJsonSuccess(data): return # Extract the token from it token = json.loads(data) return token['token'] # Insert your args here username = 'abc' password = '***' serverName = 'https://...com' serverPort = 1234 token = getToken(username, password, serverName, serverPort) print (token)
Thanks for the standalone getToken sample. Unfortunately that script is failing with the same error message. If I wanted to just use the generateToken call from a web browser with the following inputs how would that relate to the inputs in the python script as they appear different to me? I see username and password as parameters but I do not see http referer or expiration in the token script.
Starting with ArcGIS Enterprise 10.9, I wanted to share that it is now possible to do this directly through the portal with administrative reports. You are able to run an item report which shows a list of all items in your portal, along with other helpful information. In particular, this is the documentation on the outputs when running item reports: Item fields. Hopefully this helps!
Thanks to @LongDinh I was able to reimplement the script using requests module. Below is the working script
# Reads the following properties from services and writes them to a comma-delimited file: # ServiceName, Folder, Type, Status, Min Instances, Max Instances, KML, # WMS, Max Records, Cluster, Cache Directory, Jobs Directory, Output Directory # encoding=utf8 import sys reload(sys) sys.setdefaultencoding('utf8') # For HTTP calls import httplib, urllib, json, requests # For system tools import sys # For reading passwords without echoing import getpass def main(argv=None): # Ask for admin/publisher user name and password username = raw_input("Enter user name: ") password = getpass.getpass("Enter password: ") # Ask for server name & port serverName = raw_input("Enter server name: ") serverPort = 6443 # Get the location and the name of the file to be created resultFile = raw_input("Output File: ") # Get a token token = getToken(username, password, serverName, serverPort) # Get the root info serverURL = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/" # This request only needs the token and the response formatting parameter params = {'token': token, 'f': 'json'} # Connect to URL and post parameters response = requests.post(serverURL, data=params) # Read response if (response.status_code != 200): print "Could not read folder information." return else: # Deserialize response into Python object dataObj = json.loads(response.text) #Store the Folders in a list to loop on folders = dataObj["folders"] #Remove the System and Utilities folders folders.remove("System") #folders.remove("Utilities") #Add an entry for the root folder folders.append("") #Create the summary file of services serviceResultFile = open(resultFile,'w') serviceResultFile.write("ServiceName,Folder,Type,Status,Min Instances,Max Instances,FeatureService,kml,wms,Max Records,Cluster,Cache Directory,Jobs Directory,Output Directory" + "\n") #Loop on the found folders and discover the services and write the service information for folder in folders: # Determine if the loop is working on the root folder or not if folder != "": folder += "/" # Build the URL for the current folder folderURL = serverName + ":"+ str(serverPort) +"/arcgis/admin/services/" + folder params = {'token': token, 'f': 'json'} # Connect to URL and post parameters response = requests.post(folderURL, data=params) # Read response if (response.status_code != 200): print "Could not read folder information." return else: # Deserialize response into Python object dataObj = json.loads(response.text) # Loop through each service in the folder for item in dataObj['services']: if item["type"] == "GeometryServer":# and folder == "": # Build the Service URL if folder: sUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"]) statusUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"]) else: sUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"]) statusUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"]) servResponse = requests.post(sUrl, data=params) # Get the response jsonOBJ = json.loads(servResponse.text) # Build the Service URL to test the running status # Submit the request to the server servStatusResponse = requests.post(statusUrl, data=params) # Obtain the data from the response jsonOBJStatus = json.loads(servStatusResponse.text) # Build the line to write to the output file ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["clusterName"]) + "," + "NA" + "," + "NA" + "," + "NA" +"\n" # Write the results to the file serviceResultFile.write(ln) elif item["type"] == "SearchServer":# and folder == "": if folder: sUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"]) statusUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"]) else: sUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"]) statusUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"]) servResponse = requests.post(sUrl, data=params) # Get the response jsonOBJ = json.loads(servResponse.text) # Submit the request to the server servStatusResponse = requests.post(statusUrl, data=params) # Get the response jsonOBJStatus = json.loads(servStatusResponse.text) # Build the line to write to the output file ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["clusterName"]) + "," + "NA" + "," + str(jsonOBJ["properties"]["jobsDirectory"]) + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n" # Write the results to the file serviceResultFile.write(ln) elif item["type"] == "ImageServer": # Build the Service URL if folder: sUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"]) else: sUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"]) # Submit the request to the server servResponse = requests.post(sUrl, data=params) # Get the response jsonOBJ = json.loads(servResponse.text) # Build the Service URL to test the running status if folder: statusUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"]) else: statusUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"]) # Submit the request to the server servStatusResponse = requests.post(statusUrl, data=params) # Get the response jsonOBJStatus = json.loads(servStatusResponse.text) # Extract the WMS properties from the response wmsProps = [imageWMS for imageWMS in jsonOBJ["extensions"] if imageWMS["typeName"] == 'WMSServer']#.items()[0][1] == 'WMSServer'] if len(wmsProps) > 0: wmsStatus = str(wmsProps[0]["enabled"]) else: wmsStatus = "NA" # Build the line to write to the output file ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + wmsStatus +"," + "NA" + "," + str(jsonOBJ["clusterName"]) + "," + str(jsonOBJ["properties"]["cacheDir"]) + "," + "NA," + str(jsonOBJ["properties"]["outputDir"]) +"\n" # Write the results to the file serviceResultFile.write(ln) elif item["type"] == "GlobeServer": # Build the Service URL if folder: sUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"]) else: sUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"]) # Submit the request to the server servResponse = requests.post(sUrl, data=params) # Get the response jsonOBJ = json.loads(servResponse.text) #Build the Service URL to test the running status if folder: statusUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"]) else: statusUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"]) # Submit the request to the server servStatusResponse = requests.post(statusUrl, data=params) # Get the response jsonOBJStatus = json.loads(servStatusResponse.text) # Build the line to write to the output file ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["properties"]["maxRecordCount"]) + "," + str(jsonOBJ["clusterName"]) + "," + str(jsonOBJ["properties"]["cacheDir"]) + "," + "NA" + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n" # Write the results to the file serviceResultFile.write(ln) elif item["type"] == "GPServer": # Build the Service URL if folder: sUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"]) else: sUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"]) # Submit the request to the server servResponse = requests.post(sUrl, data=params) # Get the response jsonOBJ = json.loads(servResponse.text) # Build the Service URL to test the running status if folder: statusUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"]) else: statusUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"]) # Submit the request to the server servStatusResponse = requests.post(statusUrl, data=params) # Get the response jsonOBJStatus = json.loads(servStatusResponse.text) # Build the line to write to the output file ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["clusterName"]) + "," + "NA" + "," + str(jsonOBJ["properties"]["jobsDirectory"]) + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n" # Write the results to the file serviceResultFile.write(ln) elif item["type"] == "GeocodeServer": # Build the Service URL if folder: sUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"]) else: sUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"]) # Submit the request to the server servResponse = requests.post(sUrl, data=params) # Get the response jsonOBJ = json.loads(servResponse.text) if folder: statusUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"]) else: statusUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"]) # Submit the request to the server servStatusResponse = requests.post(statusUrl, data=params) # Get the response jsonOBJStatus = json.loads(servStatusResponse.text) # Build the line to write to the output file ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["clusterName"]) + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n" # Write the results to the file serviceResultFile.write(ln) elif item["type"] == "GeoDataServer": # Build the Service URL if folder: sUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"]) else: sUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"]) # Submit the request to the server servResponse = requests.post(sUrl, data=params) # Get the response jsonOBJ = json.loads(servResponse.text) if folder: statusUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"]) else: statusUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"]) # Submit the request to the server servStatusResponse = requests.post(statusUrl, data=params) # Get the response jsonOBJStatus = json.loads(servStatusResponse.text) # Build the line to write to the output file ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "NA" + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["properties"]["maxRecordCount"]) + "," + str(jsonOBJ["clusterName"]) + "," + "NA" + "," + "NA" + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n" # Write the results to the file serviceResultFile.write(ln) elif item["type"] == "MapServer": # Build the Service URL if folder: sUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s%s.%s" %(folder,item["serviceName"], item["type"]) else: sUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s.%s" %(item["serviceName"], item["type"]) # Submit the request to the server servResponse = requests.post(sUrl, data=params) # Get the response jsonOBJ = json.loads(servResponse.text) # Build the Service URL to test the running status if folder: statusUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s%s.%s/status" %(folder,item["serviceName"], item["type"]) else: statusUrl = serverName + ":"+ str(serverPort) + "/arcgis/admin/services/%s.%s/status" %(item["serviceName"], item["type"]) # Submit the request to the server servStatusResponse = requests.post(statusUrl, data=params) # Get the response jsonOBJStatus = json.loads(servStatusResponse.text) # Check for Map Cache isCached = jsonOBJ["properties"]["isCached"] if isCached == "true": cacheDir = str(jsonOBJ["properties"]["cacheDir"]) else: cacheDir = jsonOBJ["properties"]["isCached"] if len(jsonOBJ["extensions"]) == 0: # Build the line to write to the output file ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + "FeatServHolder" + "," + "Disabled" + "," + "Disabled" +"," + str(jsonOBJ["properties"]["maxRecordCount"]) + "," + str(jsonOBJ["clusterName"]) + "," + cacheDir + "," + "NA" + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n" else: # Extract the KML properties from the response kmlProps = [mapKML for mapKML in jsonOBJ["extensions"] if mapKML["typeName"] == 'KmlServer']#.items()[0][1] == 'KmlServer'] # Extract the WMS properties from the response wmsProps = [mapWMS for mapWMS in jsonOBJ["extensions"] if mapWMS["typeName"] == 'WMSServer']#.items()[0][1] == 'WMSServer'] # Extract the FeatureService properties from the response featServProps = [featServ for featServ in jsonOBJ["extensions"] if featServ["typeName"] == 'FeatureServer']#.items()[0][1] == 'FeatureServer'] if len(featServProps) > 0: featureStatus = str(featServProps[0]["enabled"]) else: featureStatus = "NA" if len(kmlProps) > 0: kmlStatus = str(kmlProps[0]["enabled"]) else: kmlStatus = "NA" if len(wmsProps) > 0: wmsStatus = str(wmsProps[0]["enabled"]) else: wmsStatus = "NA" ln = str(jsonOBJ["serviceName"]) + "," + folder + "," + str(item["type"]) + "," + jsonOBJStatus['realTimeState'] + "," + str(jsonOBJ["minInstancesPerNode"]) + "," + str(jsonOBJ["maxInstancesPerNode"]) + "," + featureStatus + "," + kmlStatus + "," + wmsStatus +"," + str(jsonOBJ["properties"]["maxRecordCount"]) + "," + str(jsonOBJ["clusterName"]) + "," + cacheDir + "," + "NA" + "," + str(jsonOBJ["properties"]["outputDir"]) +"\n" # Write the results to the file serviceResultFile.write(ln) else: print("All layers traversed") # Close the file serviceResultFile.close() def getToken(username, password, serverName, serverPort): # Token URL is typically http://server[:port]/arcgis/admin/generateToken tokenURL = "/arcgis/admin/generateToken" params = {'username': username, 'password': password, 'client': 'requestip', 'f': 'json'} url = serverName + ":"+ str(serverPort) + tokenURL # Post parameters to url response = requests.post(url, data=params) # Read response if (response.status_code != 200): print "Error while fetching tokens from admin URL. Please check the URL and try again." return else: # Extract the token from it token = response.json() return token['token'] if __name__ == "__main__": sys.exit(main(sys.argv[1:]))
Hope it helps
Note: For those like me who didn't knew how to run the script in Windows environment. Just run you Command Prompt as an Administrator and navigate to your Python installation folder, for me it was "C:\Python27\ArcGIS10.6", and run the command as "python.exe "<your-saved-directory>\service_csv.py"
Great work @arahman_mdmajid !
One last tip from me. When you see yourself copying and pasting code blocks you should consider refactoring the code block so that its logic can be reused. This also improves readability as the logic can be named appropriately based on its purpose (i.e. function)
A good example to refactor could be create a function to return your sURL and statusUrl variables. Both variables have 3 inputs (i.e. folder, item["serviceName"], item["type"]) and return a constructed url. E.g:
def constructUrl(serviceName, serviceType,folder=None): """Construct a url for this program""" if folder: url = "https://....' else: url = "https://....' return url
Best of luck with future programs!
I just fixed a similar problem - i changed all instances of HTTPConnection to HTTPSConnection and script started working
I left httpConn as it was
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.