上文我们实现了 post 请求
那么接下来,我们来看 get 和 post 的一起运用
# 导包
# http 请求库,用于 get 和 post 请求 | |
import requests | |
# json 的库,用来发送和解析 json 数据 | |
import json | |
# 别发送太快,用来延时 | |
import time | |
# 正则表达式 | |
import re |
# 数据
# 准备数据 | |
NULL="" | |
# 提取出来创建任务的开始时间和分钟 | |
hour = 0 | |
min = 0 | |
# Authorization 和 Cookie 经常变动,所以提出来 | |
Authorization = \ | |
'Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImQ3YTQ3MjgxLTBhODAtNDkxNC1iNTAxLTA2MzI4YzMwMTdkZCJ9.LCkOJtuaRCbRnzmE5k0G0Ju-VWsYblsIfQY282iy531z7bk23oJ029FgoaEONs-ajWL1uPxnFt6ZzcWX9Ufg-w' | |
Cookie = \ | |
'Admin-Token=eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImQ3YTQ3MjgxLTBhODAtNDkxNC1iNTAxLTA2MzI4YzMwMTdkZCJ9.LCkOJtuaRCbRnzmE5k0G0Ju-VWsYblsIfQY282iy531z7bk23oJ029FgoaEONs-ajWL1uPxnFt6ZzcWX9Ufg-w; sidebarStatus=0' | |
# 设置请求头 | |
addheader ={ | |
"Accept":"application/json, text/plain, */*", | |
"Accept-Encoding":"gzip, deflate", | |
"Accept-Language":"zh-CN,zh;q=0.9,en;q=0.8", | |
'Authorization':Authorization, | |
"Connection":"keep-alive", | |
"Content-Length":"0", | |
'Cookie':Cookie, | |
"Host":"139.196.207.98", | |
"Origin":"http://139.196.207.98", | |
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36", | |
} | |
adddata={ | |
} | |
ControllerManageID = 217 | |
# 链接 get 请求获取的链接 | |
url = "http://139.196.207.98/prod-api/tms/task/group/list?pageNum=1&pageSize=50&manageId="+str(ControllerManageID); | |
# post 请求的链接 | |
# http://139.196.207.98/prod-api/tms/task/group/sendTaskGroupCmdAdd/1693542006269685761 | |
addtaskurl = 'http://139.196.207.98/prod-api/tms/task/group/sendTaskGroupCmdAdd/' |
# 函数
# 先获取任务列表信息 | |
def gettaskid(): | |
# 发送 GET 请求,获取任务列表信息 | |
response = requests.get(url,data =json.dumps(adddata), headers=addheader) | |
# 打印响应结果 | |
print(response.content) | |
# 转为 json 数据进行解析 | |
json_data = json.loads(response.content) | |
# 判断状态 200 为成功 | |
if json_data["code"]!=200: | |
print("失败啦") | |
return False | |
global idlist | |
idlist = json_data["rows"] | |
for idl in idlist: | |
print(idl["id"]) | |
return True | |
# 发送 post 请求 | |
def bandtask(): | |
for idl in idlist: | |
print(addtaskurl+idl["id"]) | |
response = requests.post(str(addtaskurl+idl["id"]),data=json.dumps(adddata), headers=addheader) | |
print(response.content) | |
time.sleep(1) |
# 执行
while(1): | |
input("任意键开始...\n") | |
if gettaskid(): | |
bandtask() | |
cmd = input("\n是否继续?N/n退出,任意键继续\n") | |
if cmd == "N" or cmd == "n": | |
break |
# 全部代码
###########################---1---################################### | |
# http 请求库,用于 get 和 post 请求 | |
import requests | |
# json 的库,用来发送和解析 json 数据 | |
import json | |
# 别发送太快,用来延时 | |
import time | |
# 正则表达式 | |
import re | |
###########################---2---################################### | |
# 准备数据 | |
NULL="" | |
# 提取出来创建任务的开始时间和分钟 | |
hour = 0 | |
min = 0 | |
# Authorization 和 Cookie 经常变动,所以提出来 | |
Authorization = \ | |
'Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImQ3YTQ3MjgxLTBhODAtNDkxNC1iNTAxLTA2MzI4YzMwMTdkZCJ9.LCkOJtuaRCbRnzmE5k0G0Ju-VWsYblsIfQY282iy531z7bk23oJ029FgoaEONs-ajWL1uPxnFt6ZzcWX9Ufg-w' | |
Cookie = \ | |
'Admin-Token=eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImQ3YTQ3MjgxLTBhODAtNDkxNC1iNTAxLTA2MzI4YzMwMTdkZCJ9.LCkOJtuaRCbRnzmE5k0G0Ju-VWsYblsIfQY282iy531z7bk23oJ029FgoaEONs-ajWL1uPxnFt6ZzcWX9Ufg-w; sidebarStatus=0' | |
# 设置请求头 | |
addheader ={ | |
"Accept":"application/json, text/plain, */*", | |
"Accept-Encoding":"gzip, deflate", | |
"Accept-Language":"zh-CN,zh;q=0.9,en;q=0.8", | |
'Authorization':Authorization, | |
"Connection":"keep-alive", | |
"Content-Length":"0", | |
'Cookie':Cookie, | |
"Host":"139.196.207.98", | |
"Origin":"http://139.196.207.98", | |
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36", | |
} | |
adddata={ | |
} | |
ControllerManageID = 217 | |
# 链接 get 请求获取的链接 | |
url = "http://139.196.207.98/prod-api/tms/task/group/list?pageNum=1&pageSize=50&manageId="+str(ControllerManageID); | |
# post 请求的链接 | |
# http://139.196.207.98/prod-api/tms/task/group/sendTaskGroupCmdAdd/1693542006269685761 | |
addtaskurl = 'http://139.196.207.98/prod-api/tms/task/group/sendTaskGroupCmdAdd/' | |
###########################---3---################################### | |
# 先获取任务列表信息 | |
def gettaskid(): | |
# 发送 GET 请求,获取任务列表信息 | |
response = requests.get(url,data =json.dumps(adddata), headers=addheader) | |
# 打印响应结果 | |
print(response.content) | |
# 转为 json 数据进行解析 | |
json_data = json.loads(response.content) | |
# 判断状态 200 为成功 | |
if json_data["code"]!=200: | |
print("失败啦") | |
return False | |
global idlist | |
idlist = json_data["rows"] | |
for idl in idlist: | |
print(idl["id"]) | |
return True | |
# 发送 post 请求 | |
def bandtask(): | |
for idl in idlist: | |
print(addtaskurl+idl["id"]) | |
response = requests.post(str(addtaskurl+idl["id"]),data=json.dumps(adddata), headers=addheader) | |
print(response.content) | |
time.sleep(1) | |
###########################---4---################################### | |
while(1): | |
input("任意键开始...\n") | |
if gettaskid(): | |
bandtask() | |
cmd = input("\n是否继续?N/n退出,任意键继续\n") | |
if cmd == "N" or cmd == "n": | |
break |