【问题标题】:How to send a series of HTTP POST requests in parallel?如何并行发送一系列 HTTP POST 请求?
【发布时间】:2022-01-03 23:36:55
【问题描述】:

我正在使用 Python 3。

我需要并行发送 POST HTTP 请求。我看到很多示例展示了如何处理 GET 调用列表。

请求发送到具有不同 POST 正文的不同 URL。

关于如何做到这一点的任何提示?

【问题讨论】:

  • 尝试使用threadingrequests 模块?似乎是最简单的方法。
  • 使用 aiohttp 模块如何工作

标签: python-3.x http


【解决方案1】:

我建议使用threadingrequests 模块。线程是并行运行post请求和请求放置post请求。

我会这样做:

import threading
import requests

def target_func(url, post_body):
  requests.post(url, json=post_body)

threads = []

json_1 = {
  # json data here
}
json_2 = {
  # json data here
}

threads.append(threading.Thread(target=target_func, args=["url here", json_1]))
threads.append(threading.Thread(target=target_func, args=["url here", json_2]))

# copy and paste for how many different requests you want

for thread in threads:
  thread.start()
  thread.join()

【讨论】:

  • 我看到它可以正常工作,但我如何获得 HTTP POST 调用的响应?
  • 其实我不太确定
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-03
  • 1970-01-01
  • 2011-11-18
  • 2016-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多