日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python中POST調用Restful接口示例_python

作者:黑伴白 ? 更新時間: 2023-04-17 編程語言

Python之POST調用Restful接口示例

# -*- coding: utf-8 -*-
import json
import re
import requests
from requests.auth import HTTPBasicAuth

# web登錄用戶
userName="admin"
# web登錄密碼
passWord="admin"
# 刪除任務節點接口URL
# 刪除任務節點 將任務從計劃流程中刪除 任務靜態信息不會刪除 任務節點產生的事件會同步刪除 任務節點后依賴將不會再依賴此任務節點產生的事件
delTaskNodeURL="http://199.188.166.110:8080/MoiaControl/rest/RestService/delTaskNode"
# 用列表存儲要刪除的任務節點清單
taskNodesList=['a','b','c']

# 定義接口調用函數 通過post調用restful接口
# 采用HTTPBasicAuth一種簡單的身份認證,它是通過http的authorization請求頭中,攜帶經過base64加密的用戶名和密碼而實現的一種認證
def request_post(url, param, user, pwd):
    try:
        headers = {'charset': 'utf-8'}
        result = requests.post(url, data=param, auth=HTTPBasicAuth(user,pwd))
        # print("STATUS_CODE:{0}".format(result.status_code))
        # print("REASON:{0}".format(result.reason))
        # print("CONTENT:{0}".format(result.content.decode()))
        text=result.content.decode()
        return text
    except Exception as e:
        print(e)

# 根據任務節點列表清單 循環刪除任務節點
for item in taskNodesList:
    # 獲取任務節點名稱 組成節點刪除接口參數
    taskNodes={"taskNodes":"['%s']" % item}
    # 開始進行任務節點刪除
    resInfo = request_post(delTaskNodeURL, taskNodes, userName, passWord)
    # 判斷任務節點刪除是否成功
    if not re.search(r"刪除任務節點成功", resInfo):
        # 將任務刪除結果的字符串轉為字典格式 并獲取刪除結果描述信息
        reason=json.loads(resInfo)["describe"]
        # 打印刪除結果
        print("刪除任務節點[%s]失敗: %s" % (item, reason))
    else:
        # 將任務刪除結果的字符串轉為字典格式 并獲取刪除結果描述信息
        reason = json.loads(resInfo)["describe"]
        # 打印刪除結果
        print("刪除任務節點[%s]成功: %s" % (item, reason))

補充:python用post訪問restful服務接口

import requests
import json
data={"ids": ["00007190","00007191"]}

url="http://XXXXX"
data_json = json.dumps(data)
headers = {'Content-type': 'application/json'}
response = requests.post(url, data=data_json, headers=headers)
print(response.text)

原文鏈接:https://blog.csdn.net/weixin_49192027/article/details/122282007

欄目分類
最近更新