您可以使用requests库来发起POST请求,并使用concurrent.futures库来实现并发请求。下面是一个示例代码:

import requests
import concurrent.futures
import time

def make_post_request(url, data):
    response = requests.post(url, data=data)
    return response

def main():
    url = "https://example.com"  # 替换为您要发起POST请求的URL
    data = {"key": "value"}  # 替换为您要发送的POST数据

    start_time = time.time()

    with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
        future_to_request = {executor.submit(make_post_request, url, data): i for i in range(100)}
        for future in concurrent.futures.as_completed(future_to_request):
            try:
                response = future.result()
                print(f"Request {future_to_request[future]} completed with status code {response.status_code}")
            except Exception as e:
                print(f"Request {future_to_request[future]} raised an exception: {e}")

    end_time = time.time()
    total_time = end_time - start_time
    print(f"Total time taken: {total_time} seconds")

if __name__ == "__main__":
    main()

此代码创建了一个具有50个线程的线程池,然后使用concurrent.futures.as_completed()方法来迭代并发的请求。请求的结果将在请求完成时打印出来。

请注意,这只是一个示例代码,您需要根据自己的需求进行适当的修改。另外,请确保不要过度请求目标服务器,以避免对服务器造成过大的负担

python 发起一个post请求10s内请求100次有50次是并发的

原文地址: https://www.cveoy.top/t/topic/iRHn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录