正如标题所说,我试图将requests.get()
结果列表传递到多进程中。脚本工作良好,除了;即使使用start()
触发Multiprocess也不会运行。
我试过的是;将变量传递给multiprocess:
global headers, domainlist
它仍然不会运行多进程,终端也不会产生任何错误消息,让我感到困惑,因为多进程触发器是好的。对不起,我的英语不好…
多线程是这种场景的理想选择,因为它受I/O限制。下面是OP原始代码的一个精简但功能齐全的变体,它演示了如何做到这一点:
from concurrent.futures import ThreadPoolExecutor
import requests
import re
HACKERTARGET = 'https://api.hackertarget.com/hostsearch'
SCHEME = 'http://'
valid_domains = []
def process(domain):
try:
url = f'{SCHEME}{domain}'
print(f'Checking -> {url}')
requests.get(url, timeout=1.0, allow_redirects=False).raise_for_status()
valid_domains.append(url)
except Exception as e:
pass
domain = input('Enter domain (without scheme): ')
try:
(r := requests.get(HACKERTARGET, params={'q': domain})).raise_for_status()
with ThreadPoolExecutor() as executor:
executor.map(process, re.findall("(.*?),", r.text))
print(valid_domains)
except Exception as e:
print(e)