【问题标题】:How manage multiple proxies in a thread environment?如何在线程环境中管理多个代理?
【发布时间】:2020-11-01 14:47:15
【问题描述】:

我希望每个线程使用不同的 IP 运行。是否可以为每个线程使用不同的 ip? 到目前为止,我无法为每个线程分配不同的 IP 地址。目标是在不被禁止的情况下进行网页抓取。

f1-> ip X

f2-> ip y

f3-> ip z

这是我的代码:

import requests
from stem.control import Controller
from stem import Signal
import json
import time  
import concurrent.futures  
from fake_useragent import UserAgent



def get_tor_session():
    # initialize a requests Session
    session = requests.Session()
    # setting the proxy of both http & https to the localhost:9050 
    # this requires a running Tor service in your machine and listening on port 9050 (by default)
    session.proxies = {"http": "socks5h://localhost:9150", "https": "socks5h://localhost:9150"}
    return session

def renew_connection():
    with Controller.from_port(port=9051) as c:
        c.authenticate()
        # send NEWNYM signal to establish a new clean connection through the Tor network
        c.signal(Signal.NEWNYM)

ua=UserAgent()
headers = {'User-Agent':ua.random}
urlA = 'https://api.ipify.org'

def serveur():
    renew_connection()
    s = get_tor_session()
    rA = s.get(urlA, headers=headers)
    print(rA.text)

def Th():
    with concurrent.futures.ThreadPoolExecutor() as executor:
        f1 = executor.submit(serveur)
        f2 = executor.submit(serveur)
        f3 = executor.submit(serveur)

Th()

这是当前的回报:

185.220.101.14
185.220.101.14
185.220.101.14

【问题讨论】:

标签: python multithreading proxy ip tor


【解决方案1】:

可能有用:

我的任务是实现自动更新多代理。

我为此使用了 Tor。下面是一个小包,如何运行多个 Tor 实例:https://github.com/detonavomek/tor-pool

我使用 macOS。还有privoxy 代理助手。这是一个 privoxy Python 包:https://github.com/detonavomek/privoxy-pool

关于如何使用它的信息在这里:https://github.com/detonavomek/privoxy-pool/blob/master/test_controller.py

安装:

pip install git+https://github.com/detonavomek/privoxy-pool.git@https://github.com/detonavomek/privoxy-pool.git

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 2020-06-23
    相关资源
    最近更新 更多