使用Python 3.x 批量删除ArcGIS Server某一文件夹下的所有服务

以往对于Server的管理大部分是以前Python2.x的版本,但是现在考虑到使用Pro较多,为Python3.x的版本,有一些http连接包的连接代码有一定变化,所以这里对相关的方法进行了整理。

1. 连接server获取token

如果想批量删除服务,则需要先连接到server并获取token。3.x环境下httplib这个包已经没了,所以更换为http.client进行连接,连接方法参考:

python 复制代码
def getToken(username, password,url):
        #urltoken = "http://localhost:6080/arcgis/admin/generateToken"
        params = urllib.parse.urlencode({'username': username, 'password': password, 'client': 'requestip', 'f': 'json'})
        headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain","User-Agent":
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"}
       
        conn = http.client.HTTPConnection(url)
        conn.request('POST', '/arcgis/admin/generateToken', params, headers)

        response = conn.getresponse()
        if (response.status != 200):
            conn.close()
            print("Error: Cannot get token!")
            return
        else:
            data = response.read()
            conn.close()
   
            token = json.loads(data)
            return token['token']

2. 获取文件夹下的服务名

目的是删除某一文件夹下的所有服务,所以需要先获取到该文件夹下有哪些服务,可以参考:

python 复制代码
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

def get_services_list(url, token, folder):
        serviceslist = []
        folderURL = "/arcgis/admin/services" + "/" + folder 
        params = urllib.parse.urlencode({'token': token, 'f': 'json'})
        headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
   
        conn = http.client.HTTPConnection(url)
        conn.request('POST', folderURL, params, headers)
   
        response = conn.getresponse()
        if (response.status != 200):
            conn.close()
            print("Cannot read folder information!")
            return
        else:
            data = response.read()
   
            if not assertJsonSuccess(data):
                print("Error when reading the folder information!" + str(data))
            else:
                print("Processing folder information successfully. Now processing services......")
   
            dataobj = json.loads(data)
   
            conn.close()
   
            for item in dataobj['services']:
                fullservicename = item['serviceName'] + "." + item['type']
                print(fullservicename)
                serviceslist.append(fullservicename)
            return serviceslist

3. 批量删除服务

删除服务Rest API相关帮助可以参考官网地址:
https://developers.arcgis.com/rest/enterprise-administration/enterprise/delete-service.htm

删除方法参考:

python 复制代码
def delete_services(full_services_list,folderURL,token):
    for service in full_services_list:
        conn = http.client.HTTPConnection("localhost:6080")

        deleteservice = folderURL+'/'+ service + '/'+ 'delete'

        params = urllib.parse.urlencode({'token': token, 'f': 'json'})
        headers = {"Content-type": "application/x-www-form-urlencoded"}
        conn.request('POST', deleteservice, params, headers)
       
        response = conn.getresponse()
        if (response.status != 200):
            conn.close()
            print("Cannot connect server to delete!")
            return
        else:
             print(response)
             conn.close()
             
             print(service + "has been deleted!")
相关推荐
zhangfeng11331 分钟前
超算/曙光DCU集群 昆山站 htc /public 目录全解
人工智能·python·机器学习
Maydaycxc1 分钟前
Excel/WPS 自动化实战:科学计数法、千张表格循环处理、打包交付的多工具对比
python·自动化·excel·wps·rpa
py小王子1 分钟前
Nature 期刊图复刻|带内嵌边缘密度的多组时序回归拟合图
python·nature·期刊图片复现
玫幽倩2 分钟前
2026盘古石取证决赛(手机取证)
python·电子取证·计算机取证·聊天软件·手机取证·fic
TechWayfarer4 分钟前
IP精准定位服务接入实战:广告投放如何用位置数据做定向策略
python·网络协议·tcp/ip·flask
codeejun5 分钟前
每日一 Go-72、分布式事务 & 一致性:本地消息表、事务消息、SAGA、TCC怎么选?
开发语言·分布式·golang
开源量化GO5 分钟前
2026年期货量化主流工具期货与期权程序化选型:统一维护能力对照
python
sycmancia7 分钟前
Qt——程序中的配置文件
开发语言·qt
赶在日落之前10 分钟前
使用conda-pack打包完整 Python 环境 + 依赖包,传到无网机器解压即用
开发语言·人工智能·python
雨落在了我的手上12 分钟前
Java数据结构(一):初识集合框架
java·开发语言