Python之POST调用Restful接口示例
import json
import re
import requests
from requests.auth import HTTPBasicAuth
userName="admin"
passWord="admin"
delTaskNodeURL="http://199.188.166.110:8080/MoiaControl/rest/RestService/delTaskNode"
taskNodesList=['a','b','c']
def request_post(url, param, user, pwd):
try:
headers = {'charset': 'utf-8'}
result = requests.post(url, data=param, auth=HTTPBasicAuth(user,pwd))
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))
|