我有一个名为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”:
这样修改您的代码,以便为每个令牌发送一个请求。
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/"
]
file_lines = open("tokens.txt", "r").readlines()
for line in file_lines:
tokens.add(line.strip())
def makerequest(url):
for token in tokens:
token_data = {"Token": token}
html = requests.post(url,stream=True, data=token_data)
print(token)
return html.content
您代码中的问题是创建令牌字典--您循环令牌,但是您总是覆盖映射到“令牌”键的值。
此外,在您的代码中还有一些不好的做法。
>
请像您所做的那样小心内联打开文件
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}')