2

以下问题看起来很像 SO 上已经存在的许多相关问题(我已经阅读了它们,但我的问题略有不同并且仍然存在)。

我使用 stem 库在 Python 3.6.1 中编写了以下代码(在 macOS Sierra 和 Ubuntu 上都经过测试)。它所做的只是创建一个新的 tor 进程(配置为使用意大利 ip),打开一个到它的控制器连接并在尝试获取新的 tor 身份并等待 30 秒后检查 IP。

我的代码只不过是干库文档的略微修改版本,因此可以正常工作。

所有代码似乎都表现良好,我没有收到任何错误/异常,但每次我得到相同的 IP(有时是第二个 IP,但在两者之间切换)

这是代码(main.py):

import stem.process
import pycurl
import io
import time

from stem.util import term
from stem.control import Controller
from stem import Signal


TOR_HOST = '127.0.0.1'
TOR_SOCKS_PORT = 9050
TOR_CONTROL_PORT = 9051
TOR_LANG = 'it'

SITE_URL = 'https://www.atagar.com/echo.php'


def print_bootstrap_lines(line):
    if "Bootstrapped " in line:
        print(term.format(line, term.Color.BLUE))


def query(url):
    output = io.BytesIO()

    conn = pycurl.Curl()
    conn.setopt(pycurl.URL, url)
    conn.setopt(pycurl.PROXY, TOR_HOST)
    conn.setopt(pycurl.PROXYPORT, TOR_SOCKS_PORT)
    conn.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5_HOSTNAME)
    conn.setopt(pycurl.WRITEFUNCTION, output.write)

    try:
        conn.perform()
        return output.getvalue().decode('ascii')
    except pycurl.error as exc:
        return "Unable to reach %s (%s)" % (url, exc)


tor = stem.process.launch_tor_with_config(
    config={
        'SocksPort': str(TOR_SOCKS_PORT),
        'ControlPort': str(TOR_CONTROL_PORT),
        'ExitNodes': '{' + TOR_LANG + '}'
    },
    init_msg_handler=print_bootstrap_lines
)

ctrl = Controller.from_port(TOR_HOST, port=TOR_CONTROL_PORT)
ctrl.authenticate()

print(query(SITE_URL))
for _ in range(10):
    ctrl.signal(Signal.NEWNYM)
    time.sleep(30)
    print(query(SITE_URL))

ctrl.close()
tor.kill()

这是程序输出:

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/tommaso/PycharmProjects/test/main.py
May 16 16:33:15.000 [notice] Bootstrapped 0%: Starting
May 16 16:33:16.000 [notice] Bootstrapped 80%: Connecting to the Tor network
May 16 16:33:17.000 [notice] Bootstrapped 85%: Finishing handshake with first hop
May 16 16:33:17.000 [notice] Bootstrapped 90%: Establishing a Tor circuit
May 16 16:33:17.000 [notice] Bootstrapped 100%: Done
IP Address: 162.220.246.230 (162.220.246.230:45631)
Locale: 

IP Address: 162.220.246.230 (162.220.246.230:33604)
Locale: 

IP Address: 5.249.145.164 (torexit-readme.balist.es:42397)
Locale: 

IP Address: 162.220.246.230 (162.220.246.230:53925)
Locale: 

IP Address: 162.220.246.230 (162.220.246.230:42953)
Locale: 

IP Address: 162.220.246.230 (162.220.246.230:60250)
Locale: 

IP Address: 162.220.246.230 (162.220.246.230:55945)
Locale: 

IP Address: 162.220.246.230 (162.220.246.230:44077)
Locale: 

IP Address: 5.249.145.164 (torexit-readme.balist.es:46375)
Locale: 

IP Address: 162.220.246.230 (162.220.246.230:33205)
Locale: 

IP Address: 5.249.145.164 (torexit-readme.balist.es:47870)
Locale: 


Process finished with exit code 0

你知道为什么我每次都得到相同的 1-2 个 IP/IP,以及如何解决它吗?我不确定这里出了什么问题。也许意大利只有两个出口节点(使用意大利 IP)?

谢谢

4

1 回答 1

2

您的代码看起来不错,并且根据您的评论和我看到的输出,它似乎正在运行。

在您在该页面上看到的意大利的 64-79 个节点中,实际上只有少数是出口(您需要查看图标标志来确定这一点)。根据https://atlas.torproject.org/#search/country:it%20flag:Exit 的说法,您认为意大利的出口要少得多。在撰写本文时,网上似乎有大约 6 个出口。

至少一个容量非常低(75 KiB/s)并且可能无法满足您的请求,而我看到的另一个不允许端口 80 或 443 上的出口流量(因此它不适合并且不会被选中你的使用)。

因此,总而言之,您只获得 2 个 IP 的事实听起来是正确的。这是网络容量问题(即在您想要的国家/地区没有理想数量的出口),而不是代码问题。

于 2017-05-16T20:56:54.770 回答