Listing the properties of the services in ARCGIS Server

3202
5
12-05-2018 06:59 AM
MANESK
by
Occasional Contributor

Hi,

I'm trying to list the properties of all the services from GIS Server using python.
Is there any sample script with validating the user and password credentials?

0 Kudos
5 Replies
JoshuaBixby
MVP Esteemed Contributor

ArcGIS API for Python | ArcGIS for Developers  is expressly designed for web-GIS activities, have you looked at the Guide or any Sample Notebooks?

UPDATE:  Sample code for listing all services and printing their JSON properties.

from getpass import getpass
from arcgis.gis.server.catalog import ServicesDirectory

url =  # GIS server base URL, i.e., https://FQDN:6443/arcgis
user = # admin username

SD = ServicesDirectory(url, username=user, password=getpass(), verify_cert=False)

for dir in [None] + SD.folders:
    for srv in SD.list(folder=dir):
        print(str(srv.properties))
JakeSkinner
Esri Esteemed Contributor

Hi Manes,

You could try something like below.  It will iterate through all the services and print the properties:

 

import urllib, urllib2, json

# Variables
username = 'siteadmin'
password = 'siteadmin'
server = "gis.esri.com"

tokenURL = 'http://{}:6080/arcgis/tokens/'.format(server)
params = {'f': 'pjson', 'username': username, 'password': password, 'client': 'requestip'}
req = urllib2.Request(tokenURL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
token = data['token']


baseUrl = "http://{}:6080/arcgis/admin/services".format(server)

catalog = json.load(urllib2.urlopen(baseUrl + "/" + "?token=" + token + "&f=json"))
print("ROOT")
services = catalog['services']
for service in services:
    if service['type']!= 'StreamServer':
        print("\t" + str(service['serviceName']))
        serviceURL = "{0}/{1}.{2}?token={3}&f=json".format(baseUrl, service['serviceName'], service['type'], token)
        serviceProperties = json.load(urllib2.urlopen(serviceURL))
        print("\t\t" + str(serviceProperties))

folders = catalog['folders']
for folderName in folders:
    if str(folderName) not in ('System', 'Utilities', 'DataStoreCatalogs'):
        print(str(folderName))
        catalog = json.load(urllib2.urlopen(baseUrl + "/" + folderName + "/" + "?token=" + token + "&f=json"))
        services = catalog['services']
        for service in services:
            print("\t" + str(service['serviceName']))
            serviceURL = "{0}/{1}/{2}.{3}?token={4}&f=json".format(baseUrl, folderName, service['serviceName'], service['type'], token)
            serviceProperties = json.load(urllib2.urlopen(serviceURL))
            print("\t\t" + str(serviceProperties))
MANESK
by
Occasional Contributor

Thanks Jake.

0 Kudos
MANESK
by
Occasional Contributor

Hi Jake.

This code is working with sample arcgis online services and posting the results. When trying with my server's site,it fetches the token and posts

error inline 19: services = catalog['services']
KeyError: 'services'.What cud be the fix!

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Under line 18, add the line:

print(catalog)

What is returned?

0 Kudos