用python实现批量post请求【别乱搞啊】

目录

包含库

准备数据

函数

发送

完整代码


 

需求

最近项目到了一定阶段了,需要搞测试。

要求添加一定数量的设备,和定时任务。大概能有百十来个。

那么看了大概的流程,基本上就是往服务器提交post请求就可以实现。

那么我们开始吧。

包含库

# http请求库,用于get和post请求
import requests
# json的库,用来发送和解析json数据
import json
# 别发送太快,用来延时
import time

准备数据

# 准备数据
# Authorization 和 Cookie 经常变动,所以提出来
Authorization = 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjU1ZTRlODRlLTM4YmItNGIzZC04OWM2LTAzZjUxOTkzYWFmOCJ9.afDD6YhU8j7imQHqyy9mEukaZkFGkDrvTgBf0_hNh-VOP4W-Zf8TQLJ-3VWpYvUViMGex29DhYAbfQ4zU5ua6A'
Cookie = 'Admin-Token=eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjU1ZTRlODRlLTM4YmItNGIzZC04OWM2LTAzZjUxOTkzYWFmOCJ9.afDD6YhU8j7imQHqyy9mEukaZkFGkDrvTgBf0_hNh-VOP4W-Zf8TQLJ-3VWpYvUViMGex29DhYAbfQ4zU5ua6A; sidebarStatus=0'
# 很离谱NULL竟然提示我未定义
NULL=""
# 定义一个时和分,先随便初始化一哈
hour = 19
min = 30
# 获取你想要的开始时间和时间间隔
def getinputrule():
    global start_h
    global inv_m
    start_h = input("输入起始的时间,时")
    inv_m = input("输入间隔的时间,分钟")
# post请求的链接
url = "http://139.196.207.98/prod-api/tms/task/group";
# post请求需要的json格式的数据
data = {
    "id": NULL,
    "manageId": "217",
    "taskType": "cycle",
    "startTime": str(hour)+":"+str(min)+":00",
    "taskCycle": "1,3,2,4,5,6,7",
    "roomGkPath": "8,21,22,23,24|8,21,22,23,25|8,21,22,23,26",
    "createTime": NULL,
    "updateTime": NULL,
    "createBy": NULL,
    "updateBy": NULL
}
# 设置请求头 Authorization是登录认证,Cookie是辨别你身份的这两个每次登录都是会变的
headers = {
    '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':'209',
    'Content-type': 'application/json;charset=UTF-8',
    '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',
}

函数

# 把数字转为时间并前补0
def timeformcheck(time):
    time_str = str(time)
    if len(time_str) < 2 : time_str = '0'+time_str
    return time_str
​
# 打印拼接的串
def printtimelist():
    # 从开始时间到24小时
    for h in range(int(start_h),24):
        # 从0分钟开始 每隔你输入的时间间隔自增
        for m in range(0,60,int(inv_m)):
            data["startTime"]=timeformcheck(h)+":"+timeformcheck(m)+":00"
            if m > 60 : break
            print(data["startTime"])
​
# 发送post请求
def sendpost():
    # 从开始时间到24小时
    for h in range(int(start_h),24):
        # 从0分钟开始 每隔你输入的时间间隔自增
        for m in range(0,60,int(inv_m)):
            data["startTime"]=timeformcheck(h)+":"+timeformcheck(m)+":00"
            if m > 60 : break
            print("开始发送:"+data["startTime"]+"\n")
​
            # 发送POST请求
            response = requests.post(url, data=json.dumps(data), headers=headers)
​
            # 打印响应结果
            print(response.content)
​
            time.sleep(1)

发送

# 开始吧
step = 0
while(1):
    # 第一步,获取输入的开始时间和时间间隔
    if step == 0:
        getinputrule()
        if start_h=="" or (int(start_h)<0) or (int(start_h)>24):
            print("您输入的数据是错误的!请输入0-24")
            step=0
            continue
        else:
            if inv_m=="" or (int(inv_m)<0) or (int(inv_m)>60):
                print("您输入的数据是错误的!请输入0-60")
                step=0
                continue
            else:
                step+=1
    # 第二步,打印出组合的队列
    if step == 1:
        printtimelist()
        step+=1
        cmd = input("是否继续?输入N退出。其他任意键继续!")
        if cmd == "N" or cmd == "n":
            break
    # 第三步,发送
    if step == 2:
        sendpost()
        step+=1
    # 第四步,是否继续
    if step == 3:
        cmd = input("\n发送完毕!是否继续?N/n退出,任意键继续\n")
        if cmd == "N" or cmd == "n":
            break
        else:
            step=0

完整代码

​
###########################---1---###################################
​
# http请求库,用于get和post请求
import requests
# json的库,用来发送和解析json数据
import json
# 别发送太快,用来延时
import time
​
###########################---2---###################################
​
# 准备数据
# Authorization 和 Cookie 经常变动,所以提出来
Authorization = 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjU1ZTRlODRlLTM4YmItNGIzZC04OWM2LTAzZjUxOTkzYWFmOCJ9.afDD6YhU8j7imQHqyy9mEukaZkFGkDrvTgBf0_hNh-VOP4W-Zf8TQLJ-3VWpYvUViMGex29DhYAbfQ4zU5ua6A'
Cookie = 'Admin-Token=eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjU1ZTRlODRlLTM4YmItNGIzZC04OWM2LTAzZjUxOTkzYWFmOCJ9.afDD6YhU8j7imQHqyy9mEukaZkFGkDrvTgBf0_hNh-VOP4W-Zf8TQLJ-3VWpYvUViMGex29DhYAbfQ4zU5ua6A; sidebarStatus=0'
# 很离谱NULL竟然提示我未定义
NULL=""
# 定义一个时和分,先随便初始化一哈
hour = 19
min = 30
# 获取你想要的开始时间和时间间隔
def getinputrule():
    global start_h
    global inv_m
    start_h = input("输入起始的时间,时")
    inv_m = input("输入间隔的时间,分钟")
# post请求的链接
url = "http://139.196.207.98/prod-api/tms/task/group";
# post请求需要的json格式的数据
data = {
    "id": NULL,
    "manageId": "217",
    "taskType": "cycle",
    "startTime": str(hour)+":"+str(min)+":00",
    "taskCycle": "1,3,2,4,5,6,7",
    "roomGkPath": "8,21,22,23,24|8,21,22,23,25|8,21,22,23,26",
    "createTime": NULL,
    "updateTime": NULL,
    "createBy": NULL,
    "updateBy": NULL
}
# 设置请求头 Authorization是登录认证,Cookie是辨别你身份的这两个每次登录都是会变的
headers = {
    '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':'209',
    'Content-type': 'application/json;charset=UTF-8',
    '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',
}
​
##########################---3---#####################################
​
# 把数字转为时间并前补0
def timeformcheck(time):
    time_str = str(time)
    if len(time_str) < 2 : time_str = '0'+time_str
    return time_str
​
# 打印拼接的串
def printtimelist():
    # 从开始时间到24小时
    for h in range(int(start_h),24):
        # 从0分钟开始 每隔你输入的时间间隔自增
        for m in range(0,60,int(inv_m)):
            data["startTime"]=timeformcheck(h)+":"+timeformcheck(m)+":00"
            if m > 60 : break
            print(data["startTime"])
​
# 发送post请求
def sendpost():
    # 从开始时间到24小时
    for h in range(int(start_h),24):
        # 从0分钟开始 每隔你输入的时间间隔自增
        for m in range(0,60,int(inv_m)):
            data["startTime"]=timeformcheck(h)+":"+timeformcheck(m)+":00"
            if m > 60 : break
            print("开始发送:"+data["startTime"]+"\n")
​
            # 发送POST请求
            response = requests.post(url, data=json.dumps(data), headers=headers)
​
            # 打印响应结果
            print(response.content)
​
            time.sleep(1)
​
##########################---4---#####################################
​
# 开始吧
step = 0
while(1):
    # 第一步,获取输入的开始时间和时间间隔
    if step == 0:
        getinputrule()
        if start_h=="" or (int(start_h)<0) or (int(start_h)>24):
            print("您输入的数据是错误的!请输入0-24")
            step=0
            continue
        else:
            if inv_m=="" or (int(inv_m)<0) or (int(inv_m)>60):
                print("您输入的数据是错误的!请输入0-60")
                step=0
                continue
            else:
                step+=1
    # 第二步,打印出组合的队列
    if step == 1:
        printtimelist()
        step+=1
        cmd = input("是否继续?输入N退出。其他任意键继续!")
        if cmd == "N" or cmd == "n":
            break
    # 第三步,发送
    if step == 2:
        sendpost()
        step+=1
    # 第四步,是否继续
    if step == 3:
        cmd = input("\n发送完毕!是否继续?N/n退出,任意键继续\n")
        if cmd == "N" or cmd == "n":
            break
        else:
            step=0
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用Python实现POST请求,可以使用requests库来发送请求。以下是一个示例代码: ```python import requests url = 'https://www.example.com/' # 替换成实际的URL headers = { 'Content-Type': 'application/x-www-form-urlencoded', } data = { 'key1': 'value1', 'key2': 'value2', } response = requests.post(url, headers=headers, data=data) print(response.content.decode('utf-8')) # 打印响应内容 ``` 在这个示例代码中,首先导入了requests库。然后,设置了请求的URL和请求头部信息。接下来,定义了要发送的数据,以字典的形式存储。最后,使用`requests.post()`函数发送POST请求,并将响应内容打印出来。请注意替换实际的URL和数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [python post请求](https://blog.csdn.net/dair6/article/details/127112344)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [python 使用requests发送POST请求](https://blog.csdn.net/qq_23730073/article/details/122857953)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FlechazoCLF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值