示例:从地图文档 (MXD) 发布地图服务

本例通过给定地图文档 (MXD) 路径和 ArcGIS Server 连接文件路径发布一个新的地图服务。连接文件的扩展名为 .ags,每次使用 ArcMap 中的目录 窗口连接到 ArcGIS Server 站点时都会创建一个连接文件。本脚本不需要管理员或发布者令牌,因为它使用的是连接文件。

要运行本例,您需要使用到自己的地图文档和连接文件的路径来替换脚本顶部的局部变量。也可以更改与服务相关联的名称、摘要和标签。

# Publishes a service to machine myserver using USA.mxd
# A connection to ArcGIS Server must be established in the
#  Catalog window of ArcMap before running this script
import arcpy

# Define local variables
wrkspc = 'C:/data'
mapDoc = arcpy.mapping.MapDocument(wrkspc + '/USA/USA.mxd')

# 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_6080 (publisher).ags'

# Provide other service details
service = 'USA'
sddraft = wrkspc + service + '.sddraft'
sd = wrkspc + service + '.sd'
summary = 'General reference map of the USA'
tags = 'USA'

# Create service definition draft
arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'ARCGIS_SERVER', con, True, None, summary, tags)

# Analyze the service definition draft
analysis = arcpy.mapping.AnalyzeForSD(sddraft)

# Print errors, warnings, and messages returned from the analysis
print "The following information was returned during analysis of the MXD:"
for key in ('messages', 'warnings', 'errors'):
  print '----' + key.upper() + '---'
  vars = analysis[key]
  for ((message, code), layerlist) in vars.iteritems():
    print '    ', message, ' (CODE %i)' % code
    print '       applies to:',
    for layer in layerlist:
        print layer.name,
    print

# Stage and upload the service if the sddraft analysis did not contain errors
if analysis['errors'] == {}:
    # Execute StageService. This creates the service definition.
    arcpy.StageService_server(sddraft, sd)

    # Execute UploadServiceDefinition. This uploads the service definition and publishes the service.
    arcpy.UploadServiceDefinition_server(sd, con)
    print "Service successfully published"
else: 
    print "Service could not be published because errors were found during analysis."

print arcpy.GetMessages()
9/15/2013