我正在尝试使用python stem连接到tor,同时尝试连接(使用经过修改的示例)它只是不起作用...这是我的代码: (我正在使用python 3.4.1)
import socket,urllib, sys, socks, 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
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( tor_cmd = "C:\Users\Nadav\Desktop\Tor Browser\Tor\\tor.exe" , config = {
'SocksPort': str(SOCKS_PORT),
'ExitNodes': '{ru}', }, init_msg_handler = print_bootstrap_lines, )
print(term.format("\nChecking our endpoint:\n", term.Attr.BOLD)) print(term.format(query("https://www.atagar.com/echo.php"), term.Color.BLUE))
tor_process.kill
答案 0 :(得分:0)
袜子端口可以与使用stem / stem.control操作tor的端口不同
希望这有助于让事情适合你:
import requesocks as requests
from stem import Signal
from stem.control import Controller
# proxies for requests
proxies = {'http': 'socks5://127.0.0.1:9150',
'https': 'socks5://127.0.0.1:9150'}
# when using the Controller
with Controller.from_port(port=9151) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM)
请注意,sock的端口与Controller的端口不同。您可以在torrc文件中找到控制器的端口(对我而言,它被称为torrc-defaults)。
看起来像这样:
# Bind to this address to listen to connections from SOCKS-speaking
# applications.
SocksPort 9150
ControlPort 9151
希望这有帮助!