首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >托尔-斯坦姆-俄罗斯与爱的联系问题

托尔-斯坦姆-俄罗斯与爱的联系问题
EN

Stack Overflow用户
提问于 2015-03-01 04:11:36
回答 2查看 1.6K关注 0票数 5

我正在努力使带着爱去俄罗斯从Stem项目中运行。

代码语言:javascript
复制
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

为了让它与python3.4一起工作,我对它做了一些修改,我还使用pysocks而不是socksipy。我从urllib开始,而不是urllib3,我也遇到了同样的问题。目前我得到的是:

代码语言:javascript
复制
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的代理设置中设置的端口号。我的一个想法是,这可能是一个时间问题。是否有可能代码等待的时间不足以让站点响应?

任何帮助得到这项工作将是非常感谢的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-01 14:26:07

下面是茎教程的一个工作版本,它使用pysocks及其sockshandler模块来避免对套接字模块进行猴子修补:

代码语言:javascript
复制
#!/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代理发出的。

票数 2
EN

Stack Overflow用户

发布于 2015-07-04 22:49:22

@J.F.Sebastian发布的答案在Python3.4上给出了错误,而且它可以修复,但是下面是使用PyCurl的工作代码,就像在茎示例中一样。

代码语言:javascript
复制
import pycurl
import stem.process
import io

SOCKS_PORT = 9150

print("Starting Tor:\n")

def print_bootstrap_lines(line):
  if "Bootstrapped " in line:
    print(line)

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

output = io.BytesIO()

curl = pycurl.Curl()
curl.setopt( pycurl.URL, 'http://www.example.com' )
curl.setopt( pycurl.PROXY, 'localhost' )
curl.setopt( pycurl.PROXYPORT, SOCKS_PORT )
curl.setopt( pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5 )
curl.setopt( pycurl.WRITEFUNCTION, output.write)
curl.setopt(pycurl.HTTPHEADER, ['X-Requested-With: XMLHttpRequest', 'referer: http://www.meendo.net/?partner=13026'])
#curl.set_option(pycurl.USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36")
curl.setopt(pycurl.FOLLOWLOCATION, 1)

curl.perform()

print("RESULT : " + output.getvalue().decode('ascii'))

tor_process.kill()  # stops tor
output.close() # free used memory
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28790000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档