python - 在python中使用多个线程顺序读取文件
问题描述
我正在尝试创建一个脚本,该脚本采用文本文件的路径,然后遍历每一行并发送请求,然后返回响应的状态代码,我正在尝试实现线程,但问题是例如 50 个线程向每行发送 50 个请求,而不是向 50 个不同的行发送 50 个请求,那么我该如何实现呢?
import requests
import os
import threading
import sys
threads = []
def checkalive():
txt_file = open("%s" % (sys.argv[1]), "r")
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0',
}
for line in file:
requests.get("https://%s/" % (line), headers=headers)
for i in range(50):
t = threading.Thread(target=checkalive)
threads.append(t)
t.start()
解决方案
推荐阅读
- rest - Dotnet核心:从Web api发送字节数组作为文件而不将其保存到磁盘
- javascript - iframe-resizer 中的错误处理?
- php - 在 Woocommerce 3 中更改订单创建时的默认结帐字段值
- javascript - 在生产代码中使用断言?(Node.js 中的 TDD)
- c# - UAC 提示时设置编辑器
- android - com.android.builder.dexing.DexArchiveBuilderException:处理失败。
- android - 我收到一个错误:找不到 com.google.android.gms.internal.zzbgl 的类文件
- makefile - Makefile.inc 不存在 plexe-veins
- javascript - BrowserSync 与 Gulp 在更改完成之前重新加载浏览器
- sql - Oracle 不返回正确的日期类型值。(使用解码功能)