首页 > 解决方案 > 在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()

标签: pythonmultithreading

解决方案


推荐阅读