This is the script we use to stop and start a geocode service. I think it is basically a cut and past of an esri one. We are using http and 6080:
"""
Stop or start ArcGIS Server geocode service
"""
# Required imports
import urllib
import urllib2
import json
import contextlib ## context manager to clean up resources when exiting a 'with' block
def get_token(adminUser, adminPass, server, port, expiration):
"""
Function to generate a token from ArcGIS Server; returns token.
http://resources.arcgis.com/en/help/arcgis-rest-api/02r3/02r3000000m5000000.htm
:param adminUser: admin user
:param adminPass: admin password
:param server: AGS name, e.g. ceav456
:param port: server port
:param expiration: token timeout
:return:
"""
# Build URL
url = "http://{}:{}/arcgis/admin/generateToken?f=json".format(server, port)
# Encode the query string
query_dict = {
'username': adminUser,
'password': adminPass,
'expiration': str(expiration), ## Token timeout in minutes; default is 60 minutes.
'client': 'requestip'
}
query_string = urllib.urlencode(query_dict)
try:
# Request the token
with contextlib.closing(urllib2.urlopen(url, query_string)) as jsonResponse:
getTokenResult = json.loads(jsonResponse.read())
## Validate result
if "token" not in getTokenResult or getTokenResult == None:
raise Exception("Failed to get token: {}".format(getTokenResult['messages']))
else:
return getTokenResult['token']
except urllib2.URLError, e:
raise Exception("Could not connect to machine {} on port {}\n{}".format(server, port, e))
# Function to start or stop a service on ArcGIS Server; returns JSON response.
## http://resources.arcgis.com/en/help/arcgis-rest-api/02r3/02r3000001s6000000.htm
def service_start_stop(server, port, svc, action, token):
"""
Function to start or stop a service on ArcGIS Server; returns JSON response.
http://resources.arcgis.com/en/help/arcgis-rest-api/02r3/02r3000001s6000000.htm
:param server: AGS name, e.g. ceav456
:param port: server port
:param svc: service name, folder, and type
:param action: start or stop service
:param token: server token
:return: json response
"""
# Build URL
url = "http://{}:{}/arcgis/admin".format(server, port)
requestURL = url + "/services/{}/{}".format(svc, action)
# Encode the query string
query_dict = {
"token": token,
"f": "json"
}
query_string = urllib.urlencode(query_dict)
# Send the server request and return the JSON response
with contextlib.closing(urllib.urlopen(requestURL, query_string)) as jsonResponse:
return json.loads(jsonResponse.read())
def service_control(ags, action):
"""
Control server services
:param ags: AGS (ArcGIS Server)
:param action: start or stop
:return: result string
"""
# Local variables
# Authentication
adminUser = r"siteadmin"
adminPass = r"sit3mngr!"
# ArcGIS Server Machine
server = ags
port = "6080"
# Services ("FolderName/ServiceName.ServiceType")
svc = "Locators/CW_Composite.GeocodeServer"
# Get ArcGIS Server token
expiration = 60 ## Token timeout in minutes; default is 60 minutes.
token = get_token(adminUser, adminPass, server, port, expiration)
# Perform action on service
jsonOuput = service_start_stop(server, port, svc, action, token)
## Validate JSON object result
if jsonOuput['status'] == "success":
result = "{} {} successful on server {}".format(action.title(), str(svc), server)
else:
result = "Failed to {} {}".format(action, str(svc))
return result
raise Exception(jsonOuput)
return result