Hi Ted, I've messed with some code that I got from esri (thanks Kevin H.) and with the various sample in the 10.1 beta 2 help for the Rest API. This is overkill for what I will need eventually, but it shows one way to 1) create a token on the fly, 2) list running services within a folder(s), 3) stop those services..<< insert your code here >>...and restarts them. I'm still cleaning it up a bit, but thought it is worth sharing as is. I really like the new api, and it eliminates those (mildly annoying) DOS windows from popping up as with the AGSSOC....although I am very greatful to those that worked on and published the AGSSOC (which I still use for 10.0, of course).all: To use, replace the server, port (if necessary), username, and password with your local info. If you want to hardcode a token, you can modify it to do that. Also, at a minimum, the "dfg_common" is one of my subfolders....you will need to replace that with one of your folders....or modify to eliminate or expand this option. I hope this helps others.''' Sample of using REST API for ArcGIS Server 10.1 to list a service folder,
and stop and start the services...it also creates a token, if needed (can be hard coded).
I have it set to work with MapServices but it in theory is can work with any.
This script basically lists the services in a folder (dfg_common in sample), stops those that are
currently running, then starts them again. This is a snippet used for testing
(and not very pratical by itself). For me, I will incorporate it with updating
data and caches. Most likely it can be cleaned up a bit.
Modified for general sample distribution. Replace the following with local info:
variable values for server, port, username (AGS site Admin), and password.
Also in the line "if folder == "dfg_common" should be modified and/or
replaced for your local site....or can be modified if you don't have
subfolder structure.
This was developed under 10.1 beta 2...not tested with pre-release yet,
which is due out any day. I will test in the next couple weeks and
update/repost if needed.
Thanks to Kevin Hibma who got me on the right track, and additional info was from
samples in the 10.1 beta ArcGIS Rest API help....
Rebecca S.
debugged using Wing Pro IDE
'''
import json, urllib2, urllib
import arcpy
server = "<your server>"
port = "6080"
username = "<AGS site admin username>"
password = "<AGS site admin password>"
baseUrl = "http://{}:{}/arcgis/rest/services".format(server, port)
def gentoken(url, username, password, expiration=60):
#code to get token from the server
query_dict = {'username': username,
'password': password,
'expiration': str(expiration),
'client': 'requestip'}
query_string = urllib.urlencode(query_dict)
#arcpy.AddMessage("query_string: " + query_string)
return json.loads(urllib.urlopen(url + "?f=json", query_string).read())['token']
def getCatalog(usermame, password):
catalog = json.load(urllib2.urlopen(baseUrl + "/" + "?f=json"))
print 'ROOT'
if "error" in catalog: return
services = catalog['services']
for service in services:
response = json.load(urllib2.urlopen(baseUrl + '/' + service['name'] + '/' + service['type'] + "?f=json"))
arcpy.AddMessage(' %s %s (%s)' % (service['name'], service['type'], 'ERROR' if "error" in response else 'SUCCESS'))
folders = catalog['folders']
for folderName in folders:
if folderName == "dfg_common": # I was filtering for a particular folder for testing
catalog = json.load(urllib2.urlopen(baseUrl + "/" + folderName + "?f=json"))
arcpy.AddMessage(folderName)
if "error" in catalog: return
services = catalog['services']
for service in services:
response = json.load(urllib2.urlopen(baseUrl + '/' + service['name'] + '/' + service['type'] + "?f=json"))
#print ' %s %s (%s)' % (service['name'], service['type'], 'ERROR' if "error" in response else 'SUCCESS')
#arcpy.AddMessage(' %s %s (%s)' % (service['name'], service['type'], 'ERROR' if "error" in response else 'SUCCESS'))
serviceName = (service['name'] + "." + service['type'] )
#arcpy.AddMessage(serviceName)
msgStop = stopService(server, serviceName, username, password)
if 'success' in msgStop:
arcpy.AddMessage("stopped " + (service['name']))
else:
arcpy.AddMessage( msgStop)
'''
--> this won't work service if the service wasn't originally started
--> to begin with....would never make the "services" list
'''
msgStart = startService(server, serviceName, username, password)
if 'success' in msgStop:
arcpy.AddMessage("started " + (service['name']))
else:
arcpy.AddMessage( msgStart)
def stopService(server, servicename, username, password, token=None, port=6080):
#code to stop the service. token can be passed in, but code below will envoke the gettoken function
if token is None:
token_url = "http://{}:{}/arcgis/admin/generateToken".format(server, port)
arcpy.AddMessage("token_url: " + token_url)
token = gentoken(token_url, username, password)
arcpy.AddMessage("token: " + token)
stop_service_url = "http://{}:{}/arcgis/admin/services/{}/stop?token={}&f=json".format(server, port, servicename, token)
#arcpy.AddMessage(stop_service_url)
msg = urllib2.urlopen(stop_service_url, ' ').read() # The ' ' forces POST
return msg
def startService(server, servicename, username, password, token=None, port=6080):
#code to start the service. token can be passed in, but code below will envoke the gettoken function
if token is None:
token_url = "http://{}:{}/arcgis/admin/generateToken".format(server, port)
token = gentoken(token_url, username, password)
start_service_url = "http://{}:{}/arcgis/admin/services/{}/start?token={}&f=json".format(server, port, servicename, token)
#arcpy.AddMessage("start_service_url: " + start_service_url)
msg = urllib2.urlopen(start_service_url, ' ').read() # The ' ' forces POST
return msg
getCatalog(username, password)