使用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!")
相关推荐
AAD5558889910 小时前
数字仪表LCD显示识别与读数:数字0-9、小数点及单位kwh检测识别实战
python
微风中的麦穗12 小时前
【MATLAB】MATLAB R2025a 详细下载安装图文指南:下一代科学计算与工程仿真平台
开发语言·matlab·开发工具·工程仿真·matlab r2025a·matlab r2025·科学计算与工程仿真
2601_9491465312 小时前
C语言语音通知API示例代码:基于标准C的语音接口开发与底层调用实践
c语言·开发语言
开源技术12 小时前
Python Pillow 优化,打开和保存速度最快提高14倍
开发语言·python·pillow
学嵌入式的小杨同学12 小时前
从零打造 Linux 终端 MP3 播放器!用 C 语言实现音乐自由
linux·c语言·开发语言·前端·vscode·ci/cd·vim
Li emily13 小时前
解决港股实时行情数据 API 接入难题
人工智能·python·fastapi
wfeqhfxz258878213 小时前
农田杂草检测与识别系统基于YOLO11实现六种杂草自动识别_1
python
mftang14 小时前
Python 字符串拼接成字节详解
开发语言·python
0思必得014 小时前
[Web自动化] Selenium设置相关执行文件路径
前端·爬虫·python·selenium·自动化
石去皿14 小时前
大模型面试通关指南:28道高频考题深度解析与实战要点
人工智能·python·面试·职场和发展