我如何从 python 中的 txt 文件中请求每个令牌

How i can make a request for every token from a txt file in python

我有一个名为 tokens.txt 的文本文件。

例如:12463,4126,6343,6345。

我想用每个令牌发送一个 post 请求并使用多线程。

出于某些原因,我的代码只从 txt 文件中获取最后一个标记,并且只使用它。

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

url_list = [
    "https://www.google.com/api/"
]
file_lines = open("tokens.txt", "r").readlines()
for line in file_lines:
    tokens = {
        'Token':line.replace('/n','')
        }

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

start = time()

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

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


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

如何为每个令牌发送一个请求?

你在做

数据=代币

那时 tokens 是最后一行的赋值。如果你想要所有的令牌,你需要做一些像j:

tokens = set()
for line file_lines:
     tokens.add(......)

你的情况tokens = {"Token": <last_token>}

像这样修改您的代码,以便可以为每个令牌发送一个请求。

tokens = set() 
'''
<- You can use list also but in this case set is better  as it will ensure only 
one request for one token even if your tokens file contains duplicate line.
'''
url_list = [
    "https://www.google.com/api/"
]

tokens = set()
with open("tokens.txt", "r") as f:
    file_lines = f.readlines()
    for line in file_lines:
        tokens.add(line.strip())

token_data = {"Token": None}
def makerequest(url):
    for token in tokens:
        token_data["Token"] = token
        html = requests.post(url,stream=True, data=token_data)
        print(token)
        # do something with html here
        # don't return or break

您的代码的问题是令牌字典的创建 - 您循环提供令牌,但您总是覆盖映射到“令牌”键的值。

此外,您的代码中存在一些不良做法。

  1. 请像您一样小心内联打开文件

    file_lines = open("tokens.txt", "r").readlines()

而是将其用作上下文管理器

with open("tokens.txt", "r") as file:
    file_lines = file.readlines()

这可以确保文件在您阅读后再次关闭 - 在您的情况下,您需要确保文件被关闭(即使在崩溃等情况下)

其次,避免在函数中使用全局变量。根据你的代码,我假设你想用每个标记查询不同的 url - 所以函数应该接受这两个作为参数。然后我会分别创建一个组合列表,例如

url_token_combs = [(url, token.strip()) for url in url_list for token in file_lines]

最后,更改您的函数以使用传递给它的参数而不是全局参数,例如:

def makerequest(url_token ):
    url , token = url_token 
    html = requests.post(url,stream=True, data=token)
    return html.content

这让您现在可以使用线程遍历您的代码:

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


def makerequest(url_token):
    url , token = url_token      
    html = requests.post(url,stream=True, data=tokens)
    print(tokens)
    return html.content


if __name__ == "__main__":
    start = time()
    url_list = [
    "https://www.google.com/api/"
    ]
    
    with  open("tokens.txt", "r") as file:
        file_lines = file.readlines()

    tokens = [{'Token':line.replace('/n','') }for line in file_lines ]

    url_tokens = [(url, token.strip()) for url in url_list for token in tokens]


    processes = []
    with ThreadPoolExecutor(max_workers=200) as executor:
        for url_token in url_tokens:
            processes.append(executor.submit(makerequest, url_token))

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


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