ArcGIS10.1python调用Admin API完成几何服务的启动

          我们知道arcgisserver10.1提供一套全新的API叫Admin API他可完成对于server本身很多的控制,例如启用安全、创建集群、添加服务器等。下面一个简单的实例说明如何使用python这个API完成几何服务的启动工作。

# 代码
import httplib, urllib, json
import sys
import getpass


def main(argv=None):
    print
    print "This tool is a sample script that starts the Geometry service on a Server."
    print
    
    username = raw_input("Enter user name: ")
    password = raw_input("Enter password: ")
    serverName = raw_input("Enter server name: ")
    serverPort = 6080
   
    # Get a token
    token = getToken(username, password, serverName, serverPort)
    if token == "":
        print "Could not generate a token with the username and password provided."
        return
   
    # Construct URL to start a service - as an example the Geometry service
    serviceStartURL = "/arcgis/admin/services/utilities/Geometry.GeometryServer/start"
   
    # This request only needs the token and the response formatting parameter
    params = urllib.urlencode({'token': token, '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", serviceStartURL, params, headers)
   
    # Read response
    response = httpConn.getresponse()
    if (response.status != 200):
        httpConn.close()
        print "Error while attempting to start the service."
        return
    else:
        data = response.read()
        httpConn.close()
       
        # Check that data returned is not an error object
        if not assertJsonSuccess(data):         
            print "Error returned by operation. " + data
        else:
            print "Operation completed successfully!"
       
        return


# A function to generate a token given username, password and the adminURL.
def getToken(username, password, serverName, serverPort):
    # Token URL is typically http://server[:port]/arcgis/admin/generateToken
    tokenURL = "/arcgis/admin/generateToken"
   
    # URL-encode the token parameters
    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"]           
       

# A function that checks that the input JSON object
#  is not an error object. 
def assertJsonSuccess(data):
    obj = json.loads(data)
    if 'status' in obj and obj['status'] == "error":
        print "Error: JSON object returns an error. " + str(obj)
        return False
    else:
        return True
# Script start
if __name__ == "__main__":
       try:
           main(sys.argv[1:])
       except Error:
           print 'error'

运行程序会提示输入服务器相关的信息,输入正确后你就可以看到服务启动的相关信息。

 

你可能感兴趣的:(python,api,object,input,Parameters,token)