Skip To Content

示例:使用服务定义发布各种服务类型

服务定义 (.sd) 可将服务封装成单个可转移文件,选择性地包含源 GIS 数据。上传服务定义工具会将服务定义上传到服务器并进行发布。可从 ArcGIS Desktop 随附的 ArcPy 站点包中调用该工具。

以下 Python 示例循环检查给定文件夹,在其中查找服务定义(不包括子文件夹),并发布到 ArcGIS Server。只需要数据位置、ArcGIS Server 连接文件路径和 ArcGIS Server 上目标文件夹的名称。服务定义可以是任何服务类型。

# Publishes all service definitions in an operating system directory
#  (excluding subfolders)
import arcpy, os
# Define path to SDs
wrkspc = "C:/data"
sdDir = wrkspc + "/SDs"
# Provide path to connection file
# To create this file, right-click a folder in the Catalog window and
#  click New > ArcGIS Server Connection
con = wrkspc + "/connections/arcgis on myserver_6443 (publisher).ags"
# Destination folder name on ArcGIS Server
serverFolder = "TestPublish"
# Loop through all items in folder
sdList = os.listdir(sdDir)
for sd in sdList:
    
    # Construct path to item
    extension = os.path.splitext(sd)[1] #Get file extension
    sdPath = os.path.join(sdDir, sd)
    # Check if item is an SD file and, if so, try to publish
    if os.path.isfile(sdPath) and extension == ".sd":    
        try:     
            arcpy.UploadServiceDefinition_server(sdPath, con, "", "", "EXISTING", serverFolder)
            print "Published " + sd + " with no errors reported."
            
        except:
            print "Could not complete publishing operation for " + sd + "."
        print arcpy.GetMessages()