繁体   English   中英

如何在 python 中使用多线程发送发布请求?

[英]How to send post requests using multi threading in python?

我正在尝试使用多线程来发送带有来自 txt 文件的令牌的帖子请求。

我只设法发送 GET 请求,如果我尝试发送 post 请求,则会导致错误。 我尝试将 GET 修改为 POST,但出现错误。

我想发送带有令牌的发布请求,并验证每个令牌的真假。 (json响应)

这是代码:

import threading
import time
from queue import Queue
import requests

file_lines = open("tokens.txt", "r").readlines() # Gets the tokens from the txt file.
for line in file_lines: 
    param={
        "Token":line.replace('/n','')
    }


def make_request(url):
    """Makes a web request, prints the thread name, URL, and 
    response text.
    """
    resp = requests.get(url)
    with print_lock:
        print("Thread name: {}".format(threading.current_thread().name))
        print("Url: {}".format(url))
        print("Response code: {}\n".format(resp.text))

def manage_queue():
    """Manages the url_queue and calls the make request function"""
    while True:

        # Stores the URL and removes it from the queue so no 
        # other threads will use it. 
        current_url = url_queue.get()

        # Calls the make_request function
        make_request(current_url)

        # Tells the queue that the processing on the task is complete.
        url_queue.task_done()

if __name__ == '__main__':

    # Set the number of threads.
    number_of_threads = 5
    
    # Needed to safely print in mult-threaded programs.
    print_lock = threading.Lock()
    
    # Initializes the queue that all threads will pull from.
    url_queue = Queue()

    # The list of URLs that will go into the queue.
    urls = ["https://www.google.com"] * 30

    # Start the threads.
    for i in range(number_of_threads):

        # Send the threads to the function that manages the queue.
        t = threading.Thread(target=manage_queue)

        # Makes the thread a daemon so it exits when the program finishes.
        t.daemon = True
        t.start()
    
    start = time.time()

    # Puts the URLs in the queue
    for current_url in urls:
        url_queue.put(current_url)

    # Wait until all threads have finished before continuing the program.
    url_queue.join()

    print("Execution time = {0:.5f}".format(time.time() - start))

我想为 txt 文件中的每个令牌发送一个发布请求。

使用 post 替换 get 时出现错误: Traceback(最近一次调用最后一次):文件“C:\Users\Creative\Desktop\multithreading.py”,第 40 行,在 url_queue = Queue() NameError: name 'Queue' is未定义 current_url = url_queue.post() AttributeError: 'Queue' object has no attribute 'post' File "C:\Users\Creative\Desktop\multithreading.py", line 22, in manage_queue

还尝试了使用龙卷风和异步的解决方案,但都没有成功。

我终于设法使用多线程进行发布请求。

如果有人看到错误或者您可以对我的代码进行改进,请随时这样做:)

import requests
from concurrent.futures import ThreadPoolExecutor, as_completed
from time import time

url_list = [
    "https://www.google.com/api/"
]
tokens = {'Token': '326729'}

def download_file(url):
    html = requests.post(url,stream=True, data=tokens)
    return html.content

start = time()

processes = []
with ThreadPoolExecutor(max_workers=200) as executor:
    for url in url_list:
        processes.append(executor.submit(download_file, url))

for task in as_completed(processes):
    print(task.result())


print(f'Time taken: {time() - start}')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM