# -*- coding: cp936 -*-# 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 module import arcpy
# Define local variables# Provide path to connection file
wrkspc ='E:/fishery project/测试/新版本/'
mapDoc = arcpy.mapping.MapDocument(wrkspc +'temp_time.mxd')# To create this file, right-click a folder in the Catalog window and# click New > ArcGIS Server Connection
out_folder_path = wrkspc
con_Filename ="connection.ags"
server_url ="http://47.98.229.222:6080/arcgis/admin"
staging_folder_path = wrkspc
username ="admin"
password ="1234"
arcpy.mapping.CreateGISServerConnectionFile("ADMINISTER_GIS_SERVICES",
out_folder_path,
con_Filename,
server_url,"ARCGIS_SERVER",False,
staging_folder_path,
username,
password,"SAVE_USERNAME")# Provide other service details
serviceName ='temp_time'
sddraft = wrkspc + serviceName +'.sddraft'
sd = wrkspc + serviceName +'.sd'
summary ='summary'
tags ='tags'# Create service definition draft
arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, serviceName,'ARCGIS_SERVER', con_Filename,True,None, summary, tags)# Analyze the service definition draft
analysis = arcpy.mapping.AnalyzeForSD(sddraft)# Print errors, warnings, and messages returned from the analysisprint"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)invars.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 errorsif 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_Filename)print"Service successfully published"else:print"Service could not be published because errors were found during analysis."print arcpy.GetMessages()
具体可以查看帮助文档# -*- coding: cp936 -*-# 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 sc...