"接收控制消息时出错(SocketClosed):Tor的控制器中的空插槽内容"

Kur*_*eek 5 python tor scrapy stem privoxy

我正在使用一个使用Tor的刮刀,在这个示例项目中有一个简化版本:https://github.com/khpeek/scraper-compose.该项目具有以下(简化)结构:

.
??? docker-compose.yml
??? privoxy
?   ??? config
?   ??? Dockerfile
??? scraper
?   ??? Dockerfile
?   ??? requirements.txt
?   ??? tutorial
?   ?   ??? scrapy.cfg
?   ?   ??? tutorial
?   ?       ??? extensions.py
?   ?       ??? __init__.py
?   ?       ??? items.py
?   ?       ??? middlewares.py
?   ?       ??? pipelines.py
?   ?       ??? settings.py
?   ?       ??? spiders
?   ?       ?   ??? __init__.py
?   ?       ?   ??? quotes_spider.py
?   ?       ??? tor_controller.py
?   ??? wait-for
?       ??? wait-for
??? tor
    ??? Dockerfile
    ??? torrc
Run Code Online (Sandbox Code Playgroud)

定义的蜘蛛quotes_spider.py是一个非常简单的蜘蛛基于Scrapy教程:

import scrapy
from tutorial.items import QuoteItem

class QuotesSpider(scrapy.Spider):
    name = "quotes"
    start_urls = ['http://quotes.toscrape.com/page/{n}/'.format(n=n) for n in range(1, 3)]

    custom_settings = {
                       'TOR_RENEW_IDENTITY_ENABLED': True,
                       'TOR_ITEMS_TO_SCRAPE_PER_IDENTITY': 5
                       }

    download_delay = 2    # Wait 2 seconds (actually a random time between 1 and 3 seconds) between downloading pages


    def parse(self, response):
        for quote in response.css('div.quote'):
            item = QuoteItem()
            item['text'] = quote.css('span.text::text').extract_first()
            item['author'] = quote.css('small.author::text').extract_first()
            item['tags'] = quote.css('div.tags a.tag::text').extract()
            yield item
Run Code Online (Sandbox Code Playgroud)

settings.py,我用线条激活了Scrapy扩展

EXTENSIONS = {
   'tutorial.extensions.TorRenewIdentity': 1,
}
Run Code Online (Sandbox Code Playgroud)

这里extensions.py

import logging
import random
from scrapy import signals
from scrapy.exceptions import NotConfigured

import tutorial.tor_controller as tor_controller

logger = logging.getLogger(__name__)

class TorRenewIdentity(object):

    def __init__(self, crawler, item_count):
        self.crawler = crawler
        self.item_count = self.randomize(item_count)    # Randomize the item count to confound traffic analysis
        self._item_count = item_count                   # Also remember the given item count for future randomizations
        self.items_scraped = 0

        # Connect the extension object to signals
        self.crawler.signals.connect(self.item_scraped, signal=signals.item_scraped)

    @staticmethod
    def randomize(item_count, min_factor=0.5, max_factor=1.5):
        '''Randomize the number of items scraped before changing identity. (A similar technique is applied to Scrapy's DOWNLOAD_DELAY setting).'''
        randomized_item_count = random.randint(int(min_factor*item_count), int(max_factor*item_count))
        logger.info("The crawler will scrape the following (randomized) number of items before changing identity (again): {}".format(randomized_item_count))
        return randomized_item_count

    @classmethod
    def from_crawler(cls, crawler):
        if not crawler.settings.getbool('TOR_RENEW_IDENTITY_ENABLED'):
            raise NotConfigured

        item_count = crawler.settings.getint('TOR_ITEMS_TO_SCRAPE_PER_IDENTITY', 50)

        return cls(crawler=crawler, item_count=item_count)          # Instantiate the extension object

    def item_scraped(self, item, spider):
        '''When item_count items are scraped, pause the engine and change IP address.'''
        self.items_scraped += 1
        if self.items_scraped == self.item_count:
            logger.info("Scraped {item_count} items. Pausing engine while changing identity...".format(item_count=self.item_count))

            self.crawler.engine.pause()

            tor_controller.change_identity()                        # Change IP address (cf. https://stem.torproject.org/faq.html#how-do-i-request-a-new-identity-from-tor)
            self.items_scraped = 0                                  # Reset the counter
            self.item_count = self.randomize(self._item_count)      # Generate a new random number of items to scrape before changing identity again

            self.crawler.engine.unpause()
Run Code Online (Sandbox Code Playgroud)

并且tor_controller.py

import logging
import sys
import socket
import time
import requests
import stem
import stem.control

# Tor settings
TOR_ADDRESS = socket.gethostbyname("tor")           # The Docker-Compose service in which this code is running should be linked to the "tor" service.
TOR_CONTROL_PORT = 9051         # This is configured in /etc/tor/torrc by the line "ControlPort 9051" (or by launching Tor with "tor -controlport 9051")
TOR_PASSWORD = "foo"            # The Tor password is written in the docker-compose.yml file. (It is passed as a build argument to the 'tor' service).

# Privoxy settings
PRIVOXY_ADDRESS = "privoxy"     # This assumes this code is running in a Docker-Compose service linked to the "privoxy" service
PRIVOXY_PORT = 8118             # This is determined by the "listen-address" in Privoxy's "config" file
HTTP_PROXY = 'http://{address}:{port}'.format(address=PRIVOXY_ADDRESS, port=PRIVOXY_PORT)

logger = logging.getLogger(__name__)


class TorController(object):
    def __init__(self):
        self.controller = stem.control.Controller.from_port(address=TOR_ADDRESS, port=TOR_CONTROL_PORT)
        self.controller.authenticate(password=TOR_PASSWORD)
        self.session = requests.Session()
        self.session.proxies = {'http': HTTP_PROXY}

    def request_ip_change(self):
        self.controller.signal(stem.Signal.NEWNYM)

    def get_ip(self):
        '''Check what the current IP address is (as seen by IPEcho).'''
        return self.session.get('http://ipecho.net/plain').text

    def change_ip(self):
        '''Signal a change of IP address and wait for confirmation from IPEcho.net'''
        current_ip = self.get_ip()
        logger.debug("Initializing change of identity from the current IP address, {current_ip}".format(current_ip=current_ip))
        self.request_ip_change()
        while True:
            new_ip = self.get_ip()
            if new_ip == current_ip:
                logger.debug("The IP address is still the same. Waiting for 1 second before checking again...")
                time.sleep(1)
            else:
                break
        logger.debug("The IP address has been changed from {old_ip} to {new_ip}".format(old_ip=current_ip, new_ip=new_ip))
        return new_ip

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self.controller.close()


def change_identity():
    with TorController() as tor_controller:
        tor_controller.change_ip()
Run Code Online (Sandbox Code Playgroud)

如果我开始使用爬网docker-compose build后跟docker-compose up,大体上扩展工作:根据日志,它成功更改IP地址并继续抓取.

然而,令我烦恼的是,在引擎暂停期间,我看到了错误消息,例如

scraper_1  | 2017-05-12 16:35:06 [stem] INFO: Error while receiving a control message (SocketClosed): empty socket content
Run Code Online (Sandbox Code Playgroud)

其次是

scraper_1  | 2017-05-12 16:35:06 [stem] INFO: Error while receiving a control message (SocketClosed): received exception "peek of closed file"
Run Code Online (Sandbox Code Playgroud)

是什么导致了这些错误?既然他们有INFO水平,也许我可以忽略它们?(我在https://gitweb.torproject.org/stem.git/看了一下Stem的源代码,但到目前为止还没有能够处理正在发生的事情).

小智 5

您可以获取茎的 Logger 实例并将其关闭,如下所示:

from stem.util.log import get_logger

logger = get_logger()
logger.propagate = False
Run Code Online (Sandbox Code Playgroud)


bta*_*aek 2

我不知道您对您的问题是否得出或得出什么结论。

我实际上收到了与您相同的日志消息。我的 Scrapy 项目表现良好,使用 Tor 和 privoxy 的 IP 轮换也成功。我只是不断地获取日志INFO : [stem ] Error while receiving a control message (SocketClosed): empty socket content,这让我很烦恼。

我花了一些时间挖掘它的原因,并看看我是否可以忽略它(毕竟,它只是一条信息消息而不是错误消息)。

最重要的是我不知道是什么原因造成的,但我觉得忽略它是足够安全的

正如日志所说,套接字内容(实际上是包含套接字连接相关信息的stem control_file)为空。当control_file为空时,它会触发关闭套接字连接(根据python套接字文档)。我不确定是什么导致 control_file 为空以关闭套接字连接。但是,如果套接字连接确实关闭,则看起来套接字连接已成功打开,因为我的 scrapy 的爬行作业和 ip 轮换工作正常。虽然我找不到真正的原因,但我只能假设几个原因:(1)Tor网络不稳定,(2)当你的代码运行时controller.signal(Signal.NEWNYM),套接字暂时关闭并再次打开,或者其他原因我现在想不出来。