通过Python连接Tor正常,但未显示为在Tor上

2 投票
1 回答
701 浏览
提问于 2025-04-18 07:38

我正在使用连接到Tor网络的示例,这个示例应该能把客户端连接到Tor网络。看起来它确实在连接,但当我检查IP地址时,发现显示的IP不对,也不是Tor的IP。你们知道这是为什么吗?更重要的是,我该如何解决这个问题呢? :)

import StringIO
import socket
import urllib

import socks  # SocksiPy module
import stem.process

from stem.util import term

SOCKS_PORT = 7000

# Set socks proxy and wrap the urllib module

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT)
socket.socket = socks.socksocket

# Perform DNS resolution through the socket

def getaddrinfo(*args):
  return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]

socket.getaddrinfo = getaddrinfo


def query(url):
  """
  Uses urllib to fetch a site using SocksiPy for Tor over the SOCKS_PORT.
  """

  try:
    return urllib.urlopen(url).read()
  except:
    return "Unable to reach %s" % url


# Start an instance of Tor configured to only exit through Russia. This prints
# Tor's bootstrap information as it starts. Note that this likely will not
# work if you have another Tor instance running.

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


print term.format("Starting Tor:\n", term.Attr.BOLD)

tor_process = stem.process.launch_tor_with_config(
  config = {
    'SocksPort': str(SOCKS_PORT),
    'ExitNodes': '{ru}',
  },
  init_msg_handler = print_bootstrap_lines,
)

我得到的输出是:

richard@Tornado:~/Documents/Masters Project$ python russiaExample.py 
Starting Tor:

May 26 21:56:49.000 [notice] Bootstrapped 80%: Connecting to the Tor network.
May 26 21:56:50.000 [notice] Bootstrapped 85%: Finishing handshake with first hop.
May 26 21:56:50.000 [notice] Bootstrapped 90%: Establishing a Tor circuit.
May 26 21:56:50.000 [notice] Bootstrapped 100%: Done.

但是当我访问https://check.torproject.org/来检查我是否在使用Tor时,它显示我现在在使用Tor,但我的正常IP地址还是显示出来了。

这个问题是怎么回事呢?因为上面的输出看起来是说它已经成功建立了与Tor的连接,但似乎并没有真正使用它?

我这样理解对吗?

谢谢大家!

1 个回答

1

你需要设置你的浏览器,让它通过Tor来上网。

如果你使用的是Firefox浏览器:

  1. 打开编辑,然后选择首选项,接着点击高级,找到“配置Firefox如何连接到互联网”的设置

  2. socks主机那一栏输入127.0.0.1,在端口那一栏输入7000

接着去whatismyip.com,你会看到一个新的IP地址。或者你可以去check tor,project,查看你是否成功使用了Tor

撰写回答