Skip to content

Instantly share code, notes, and snippets.

@green3g
Created June 9, 2020 16:24
Show Gist options
  • Save green3g/e00cfa2b8b056f00bd907bf7cec1bd3e to your computer and use it in GitHub Desktop.
Save green3g/e00cfa2b8b056f00bd907bf7cec1bd3e to your computer and use it in GitHub Desktop.
Stop arcgis services
#-------------------------------------------------------------------------------
# Name: ToggleServices
# Purpose: to demo the rest api of arcgis server
#
# Author: mvanhulzen
#
# Created: 15-10-2018
# Copyright: (c) Esri Nederland BV 2018
#-------------------------------------------------------------------------------
import requests
import datetime
username ='username'
password = 'secret'
_serverurl = "https://localhost:6443/arcgis/admin"
_action = "stop"; #can be start or stop
def run_action(url, token):
print(url)
r = requests.get('{}?f=json&token={}'.format(url, token), verify=False)
services = r.json()
print(services)
#for a list of all services from the server
for service in services["services"]:
if service== services["services"][2]:
print("We are not going to stop our {}".format(service["serviceName"]))
else:
print("Set service {} to {}".format(service["serviceName"],_action))
serviceActionUrl = "{}/{}.{}/{}?f=json&token={}".format(url,service["serviceName"],service["type"],_action,token)
r = requests.post(serviceActionUrl, verify=False)
print("result: {}".format(r.text))
def main():
#generate a token on arcgis for server
token = GetToken()
url = '{}/services'.format(_serverurl)
r = requests.get('{}?f=json&token={}'.format(url,token), verify=False)
service_json = r.json()
run_action
for folder in service_json['folders']:
run_action('{}/{}'.format(url, folder), token)
print("Script complete")
def GetToken():
token_url = "{}/generateToken?f=json".format(_serverurl)
token_params = {'username':username,'password': password,'client': 'requestip','f':'json','expiration':60}
print("requesting token with username: {}".format(username))
r = requests.post(token_url,token_params, verify=False)
print("Resultcode: {}".format(r.text))
token_obj= r.json()
token = token_obj['token']
expires = token_obj['expires']
tokenExpires = datetime.datetime.fromtimestamp(int(expires)/1000)
print("token for user {}, valid till: {} : {}".format(username,tokenExpires,token))
return token
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment