Skip To Content

示例:创建站点

本示例介绍了如何根据用户提供的值(主站点管理员帐户、配置存储位置和服务器目录的根位置)创建 ArcGIS Server 站点。这些值与在 ArcGIS Server 管理器中手动创建站点时要求用户输入的值完全相同。

通过简单传入所需的主站点管理员的用户名和密码以及服务器名称和端口号,仅使用较少代码即可调用 ArcGIS REST API 中的 createNewSite 操作。在下例中,可灵活地定义所需配置存储位置和根服务器目录位置。大多数代码与创建目录相关联。

通过编程方式定义服务器目录时,必须将用于详细描述目录属性的大量 JavaScript 对象表示法 (JSON) 传入 createNewSite 操作。在下例中,将使用 Python 字典配置目录属性。然后,通过使用 Python 的 json.dumps() 函数将这些字典序列化到 JSON 中。这有助于保持示例的可读性。

下面所使用的目录属性与创建站点时 ArcGIS Server 管理器应用的默认属性完全相同。您可根据喜好修改这些属性。

# Demonstrates how to create a new site

# For Http calls
import httplib, urllib, json

# For system tools
import sys, os

# For reading passwords without echoing
import getpass

# Defines the entry point into the script
def main(argv=None):
  
    # Ask for admin/publisher user name and password
    username = raw_input("Enter desired primary site administrator name: ")
    password = getpass.getpass("Enter desired primary site administrator password: ")

    # Ask for server name
    serverName = raw_input("Enter server name: ")
    serverPort = 6080 

    # Ask for config store and root server directory paths
    configStorePath = raw_input("Enter config store path: ")
    rootDirPath = raw_input("Enter root server directory path: ")
    
    # Set up required properties for config store
    configStoreConnection={"connectionString": configStorePath, "type": "FILESYSTEM"}
 
    # Set up paths for server directories             
    cacheDirPath = os.path.join(rootDirPath, "arcgiscache")
    jobsDirPath = os.path.join(rootDirPath, "arcgisjobs")
    outputDirPath = os.path.join(rootDirPath, "arcgisoutput")
    systemDirPath = os.path.join(rootDirPath, "arcgissystem")
   
    # Create Python dictionaries representing server directories
    cacheDir = dict(name = "arcgiscache",physicalPath = cacheDirPath,directoryType = "CACHE",cleanupMode = "NONE",maxFileAge = 0,description = "Stores tile caches used by map, globe, and image services for rapid performance.", virtualPath = "")    
    jobsDir = dict(name = "arcgisjobs",physicalPath = jobsDirPath, directoryType = "JOBS",cleanupMode = "TIME_ELAPSED_SINCE_LAST_MODIFIED",maxFileAge = 360,description = "Stores results and other information from geoprocessing services.", virtualPath = "")
    outputDir = dict(name = "arcgisoutput",physicalPath = outputDirPath,directoryType = "OUTPUT",cleanupMode = "TIME_ELAPSED_SINCE_LAST_MODIFIED",maxFileAge = 10,description = "Stores various information generated by services, such as map images.", virtualPath = "")
    systemDir = dict(name = "arcgissystem",physicalPath = systemDirPath,directoryType = "SYSTEM",cleanupMode = "NONE",maxFileAge = 0,description = "Stores files that are used internally by the GIS server.", virtualPath = "")
 
    # Serialize directory information to JSON    
    directoriesJSON = json.dumps(dict(directories = [cacheDir, jobsDir, outputDir, systemDir]))      
  
    # Construct URL to create a new site
    createNewSiteURL = "/arcgis/admin/createNewSite"
    
    # Set up parameters for the request
    params = urllib.urlencode({'username': username, 'password': password, 'configStoreConnection': 

configStoreConnection, 'directories':directoriesJSON, '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", createNewSiteURL, params, headers)
    
    # Read response
    response = httpConn.getresponse()
    if (response.status != 200):
        httpConn.close()
        print "Error while creating the site."
        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. " + str(data)
        else:
            print "Site created successfully"
     
        return
    

# 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__":
    sys.exit(main(sys.argv[1:]))