并发编程 六 - 支付接口并发

支付接口并发

需求:对支付接口做并发,验证账户金额的扣款(-)冻结(+),然后把执行结果写到一个日志文件

# @Time    :  '2021-6-19 07:58'
# @Author  :  'pc.kang'
import time,json,requests
from threading import Thread,Lock

payid_list = {11111,
22222,
33333,
44444,
55555,
66666,
77777,
88888,
99999,
00000}
url = "http://xxx.com/api/Pay.do"
headers = {
    "Connection": "keep-alive",
    "Content-Length": "23",
    "Accept": "application/json, text/plain, */*",
    "X-Requested-With": "XMLHttpRequest",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36",
    "Content-Type": "application/json",
    "Origin": "http://xxx.com",
    "Referer": "http://xxx/api/",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
    "Cookie": "11379a8f66bf4fea9c2b8620642e33b0"
}
def run(lock):
    for num in payid_list:
        param = {"id": num}
        body = json.dumps(param)
        lock.acquire()
        res = requests.post(url=url, data=body, headers=headers)
        print(res.text)
        result = json.loads(str(res.text))
        # if(result["code"] == "0"):
        #     print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "  付款单id: %s 付款成功 \n" % num)
        # else:
        #     print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "  付款单id: %s 付款失败,错误信息:%s \n" %(num,res.text))
        if(result["code"] == "0"):
            with open("log.txt", "a", encoding="utf8") as fp:
                fp.write(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "  付款单id: %s 付款成功 \n" %num)
        else:
            with open("log.txt", "a", encoding="utf8") as fp:
                fp.write(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "  付款单id: %s 付款失败,错误信息:%s \n" %(num,res.text))
        lock.release()

if __name__=="__main__":
    lock = Lock()
    t_list = [Thread(target=run,args=(lock,))]
    print(t_list)
    for t in t_list:
        t.start()
    for t in t_list:
        t.join()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我是一言

让我看看你的头像

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

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

打赏作者

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

抵扣说明:

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

余额充值