服务定义文件 (.sd) 可将服务封装成单个可转移文件,选择性地包含源 GIS 数据。 上传服务定义地理处理工具会将服务定义上传到 ArcGIS Server 站点并进行发布。 您可从 ArcGIS Server 随附的 ArcPy 站点包中调用该工具。
以下 Python 示例将循环遍历 Microsoft Windows 计算机上的给定文件夹,查找该文件夹中包含的服务定义(不包括子文件夹),并将其发布到使用连接文件 (.ags) 指定的独立 ArcGIS Server 站点。
运行该工具需要一个包含服务定义文件的本地数据位置、ArcGIS Server 连接文件的路径以及 ArcGIS Server 站点上目标文件夹的名称。
# Publishes all service definitions in an operating system directory
# (excluding subfolders). Services are published but not started.
# Import required modules
import arcpy
import os
# Define path to SD files on local machine
sdDir = "C:/data/sd_files"
# Provide path to ArcGIS Server connection file
con = "C:/projects/publishing_project/arcgis on myserver_6443.ags"
# Destination folder name on ArcGIS Server
serverFolder = "ScriptPublish"
# Loop through all items in local directory
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, "STOPPED")
print("Published " + sd + " with no errors reported.")
except:
print("Could not complete publishing operation for " + sd + ".")
print(arcpy.GetMessages())