gpt4 book ai didi

python - Tor Stem - 俄罗斯与爱情连接问题

转载 作者:太空狗 更新时间:2023-10-30 01:28:43 24 4
gpt4 key购买 nike

我正在尝试获取 To Russia With Love tutoial来自 Stem 项目工作。

from io import StringIO
import socket
import urllib3
import time

import socks # SocksiPy module
import stem.process

from stem.util import term

SOCKS_PORT = 9150

# 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 urllib3.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(
tor_cmd = "C:\Tor Browser\Browser\TorBrowser\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() # stops tor

我对原始版本进行了一些微调,使其可以与 python 3.4 一起使用,并且我还使用 pysocks 而不是 socksipy。我从 urllib 而不是 urllib3 开始,我遇到了同样的问题。目前我得到:

C:\Python>python program1.py
←[1mStarting Tor:
←[0m
←[34mFeb 28 21:59:45.000 [notice] Bootstrapped 0%: Starting←[0m
←[34mFeb 28 21:59:45.000 [notice] Bootstrapped 5%: Connecting to directory server←[0m
←[34mFeb 28 21:59:45.000 [notice] Bootstrapped 80%: Connecting to the Tor network←[0m
←[34mFeb 28 21:59:45.000 [notice] Bootstrapped 85%: Finishing handshake with first hop←[0m
←[34mFeb 28 21:59:46.000 [notice] Bootstrapped 90%: Establishing a Tor circuit←[0m
←[34mFeb 28 21:59:47.000 [notice] Bootstrapped 100%: Done←[0m
←[1m
Checking our endpoint:
←[0m
←[34mUnable to reach https://www.atagar.com/echo.php←[0m

我在 tor 之外有过类似的代码工作。我可以将我的 Tor 浏览器连接到该站点,并且可以毫无问题地浏览到它。我试过更改端口号,但这是在 Tor 的代理设置中设置的。我的一个想法是,这可能是一个时间问题。有没有可能是代码等待网站响应的时间不够长?

如能提供帮助,我们将不胜感激。

最佳答案

这是 the stem tutorial 的工作版本使用 pysocks及其 sockshandler 模块以避免猴子修补套接字模块:

#!/usr/bin/env python
"""
https://stem.torproject.org/tutorials/to_russia_with_love.html

Usage:
russian-tor-exit-node [<tor>] [--color] [--geoipfile=</path/to/file>]
russian-tor-exit-node -h | --help
russion-tor-exit-node --version

Dependencies:

- tor (packaged and standalone executables work)
- pip install stem
- pip install PySocks
- pip install docopt
: parse options
- pip install colorama
: cross-platform support for ANSI colors
- [optional] sudo apt-get tor-geoipdb
: if tor is bundled without geoip files; --geoipfile=/usr/share/tor/geoip
"""
import sys
from contextlib import closing

import colorama # $ pip install colorama
import docopt # $ pip install docopt
import socks # $ pip install PySocks
import stem.process # $ pip install stem
from sockshandler import SocksiPyHandler # see pysocks repository
from stem.util import term

try:
import urllib2
except ImportError: # Python 3
import urllib.request as urllib2


args = docopt.docopt(__doc__, version='0.2')
colorama.init(strip=not (sys.stdout.isatty() or args['--color']))

tor_cmd = args['<tor>'] or 'tor'
socks_port = 7000
config = dict(SocksPort=str(socks_port), ExitNodes='{ru}')
if args['--geoipfile']:
config.update(GeoIPFile=args['--geoipfile'], GeoIPv6File=args['--geoipfile']+'6')


def query(url, opener=urllib2.build_opener(
SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, "localhost", socks_port))):
try:
with closing(opener.open(url)) as r:
return r.read().decode('ascii')
except EnvironmentError as e:
return "Unable to reach %s: %s" % (url, e)

# 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))
else:
print(line)

print(term.format("Starting Tor:\n", term.Attr.BOLD))
tor_process = stem.process.launch_tor_with_config(
tor_cmd=tor_cmd,
config=config,
init_msg_handler=print_bootstrap_lines,
)
try:
print(term.format("\nChecking our endpoint:\n", term.Attr.BOLD))
print(term.format(query("https://icanhazip.com"), term.Color.BLUE))
finally:
if tor_process.poll() is None: # still running
tor_process.terminate() # stops tor
tor_process.wait()

它适用于我的 Ubuntu 机器上的 Python 2 和 3。

strace 显示数据和 dns 请求是通过 tor 代理发出的。

关于python - Tor Stem - 俄罗斯与爱情连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28790000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com