我想为代理编写检查器,但在使用多线程时遇到了问题。
代码看起来像这样
检查类
import requests as req
class Checker():
def __init__(self, normal_status_code=200, check_address="http://google.com"):
self.check_address = check_address
self.normal_status_code = normal_status_code
def check_proxy(self, proxy_dict):
try:
r = req.get(self.check_address, proxies=proxy_dict)
except req.exceptions.ProxyError:
return False
if r.status_code == self.normal_status_code:
return True
else:
return False
因此,如果为每个代理端口集调用检查代理方法,那么我们将获得正常代理的列表。但这会运行得很慢,所以我使用线程库
from Checker import Checker
import threading
def check(proxy):
"""Функция для работы с многопоточностью"""
print(checker.check_proxy(proxy))
threads = 10
checker = Checker()
proxy_filename = "proxy.txt"
proxy_list = []
try:
f = open(proxy_filename, "r")
except FileNotFoundError:
filename = input("Введите название файла прокси ")
f = open(filename, "r")
for line in f:
proxy = "http://" + line
proxy = proxy.replace("\n", "")
proxy_list.append({"http": proxy})
print(proxy_list)
for i, proxy_dict in enumerate(proxy_list):
t = threading.Thread(target=check, name=f"thread {i}", args=proxy_dict)
t.start()
此代码给出以下错误:
[{'http': 'http://130.185.122.203:3128'}]
Exception in thread thread 0:
Traceback (most recent call last):
File "C:\Users\Alex\AppData\Local\Programs\Python\Python39\lib\threading.py", line 950, in _bootstrap_inner
self.run()
File "C:\Users\Alex\AppData\Local\Programs\Python\Python39\lib\threading.py", line 888, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Alex\PycharmProjects\Proxy\main.py", line 7, in check
print(checker.check_proxy(proxy))
File "C:\Users\Alex\PycharmProjects\Proxy\Checker.py", line 21, in check_proxy
r = req.get(self.check_address, proxies=proxy_dict)
File "C:\Users\Alex\PycharmProjects\Proxy\venv\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\Alex\PycharmProjects\Proxy\venv\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\Alex\PycharmProjects\Proxy\venv\lib\site-packages\requests\sessions.py", line 532, in request
settings = self.merge_environment_settings(
File "C:\Users\Alex\PycharmProjects\Proxy\venv\lib\site-packages\requests\sessions.py", line 710, in merge_environment_settings
no_proxy = proxies.get('no_proxy') if proxies is not None else None
AttributeError: 'str' object has no attribute 'get'
Process finished with exit code 0
问题是您
Thread
错误地将线程函数参数传递给构造函数。args
构造函数将参数视为Thread
序列,因此,如果函数参数是唯一的,则必须在容器中传递,例如,在元组中:此示例演示了差异: