A service definition file (.sd) encapsulates a service into a single transferable file, optionally including source GIS data. The Upload Service Definition geoprocessing tool uploads a service definition to the ArcGIS Server site and publishes it. You can invoke the tool from the ArcPy site package included with ArcGIS Server.
The following Python example loops through a given folder on a Microsoft Windows machine, finds the service definitions contained in the folder (excluding subfolders), and publishes them to the stand-alone ArcGIS Server site specified using the connection file (.ags).
Running the tool requires a local data location containing the service definition files, the path to the ArcGIS Server connection file, and the name of the destination folder on the ArcGIS Server site.
# 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())