Skip To Content

示例:防止在发布时复制数据

该脚本阻止在发布时将数据复制到服务器。这一阻止数据复制的选项自 ArcGIS 10.1 Service Pack 1 起可用。

将项目(如地图文档)发布到 ArcGIS Server 时,服务器将检查文档中引用的所有数据源,以确定这些数据源是否已注册到 ArcGIS Server。在默认情况下,任何来自未注册数据源的数据都将复制到服务器并放置在服务器目录中。作为服务器管理员,您可能希望阻止数据复制,以强制实施数据注册,并防止数据集在服务器中累积。

您只需在服务器上运行一次此脚本即可阻止所有数据复制。

# Demonstrates how to disable automatic data copying when publishing to the server
# For Http calls
import httplib, urllib, json

# For system tools
import sys

# For reading passwords without echoing
import getpass

# Defines the entry point into the script
def main(argv=None):
    # Print some info
    print
    print "This sample script disables automatic data copying when publishing to the server"
    print
    
    # Ask for admin/publisher user name and password
    username = raw_input("Enter user name: ")
    password = getpass.getpass("Enter password: ")

    # Ask for server name
    serverName = raw_input("Enter server name: ")
    serverPort = 6080
    
    # Get a token
    token = getToken(username, password, serverName, serverPort)
    if token == "":
        print "Could not generate a token with the username and password provided."
        return
    
    # Construct URL to start a service - as an example the Geometry service
    UpdateDatastoreConfigurationURL = "/arcgis/admin/data/config/update"
    
    # This request only needs the token and the response formatting parameter 
    params = urllib.urlencode({'token': token, 'f': 'json','datastoreConfig':'{"blockDataCopy": true}'})
    
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    
    # Connect to URL and post parameters    
    httpConn = httplib.HTTPConnection(serverName, serverPort)
    httpConn.request("POST", UpdateDatastoreConfigurationURL, params, headers)
    
    # Read response
    response = httpConn.getresponse()
    if (response.status != 200):
        httpConn.close()
        print "Error while attempting to change the data store configuration."
        return
    else:
        data = response.read()
        httpConn.close()
        
        # Check that data returned is not an error object
        if not assertJsonSuccess(data):          
            print "Error returned by operation. " + data
        else:
            print "Operation completed successfully!"
        
        return


# A function to generate a token given username, password and the adminURL.
def getToken(username, password, serverName, serverPort):
    # Token URL is typically http://server[:port]/arcgis/admin/generateToken
    tokenURL = "/arcgis/admin/generateToken"
    
    # URL-encode the token parameters
    params = urllib.urlencode({'username': username, 'password': password, 'client': 'requestip', 'f': 'json'})
    
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    
    # Connect to URL and post parameters
    httpConn = httplib.HTTPConnection(serverName, serverPort)
    httpConn.request("POST", tokenURL, params, headers)
    
    # Read response
    response = httpConn.getresponse()
    if (response.status != 200):
        httpConn.close()
        print "Error while fetching tokens from admin URL. Please check the URL and try again."
        return
    else:
        data = response.read()
        httpConn.close()
        
        # Check that data returned is not an error object
        if not assertJsonSuccess(data):            
            return
        
        # Extract the token from it
        token = json.loads(data)        
        return token["token"]            
        

# A function that checks that the input JSON object 
#  is not an error object.  
def assertJsonSuccess(data):
    obj = json.loads(data)
    if 'status' in obj and obj['status'] == "error":
        print "Error: JSON object returns an error. " + str(obj)
        return False
    else:
        return True
    
        
# Script start
if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))