How to get the Tor ExitNode IP with Python and Stem

倖福魔咒の 提交于 2021-02-08 07:42:35

问题


I'm trying to get the external IP that Tor uses, as mentioned here. When using something like myip.dnsomatic.com, this is very slow. I tried what was suggested in the aforementioned link (python + stem to control tor through the control port), but all you get is circuit's IPs with no assurance of which one is the one on the exitnode, and, sometimes the real IP is not even among the results.

Any help would be appreciated.

Also, from here, at the bottom, Amine suggests a way to renew the identity in Tor. There is an instruction, controller.get_newnym_wait(), which he uses to wait until the new connection is ready (controller is from Control in steam.control), isn't there any thing like that in Steam (sorry, I checked and double/triple checked and couldn't find nothing) that tells you that Tor is changing its identity?


回答1:


You can get the exit node ip without calling a geoip site.

This is however on a different stackexchange site here - https://tor.stackexchange.com/questions/3253/how-do-i-trap-circuit-id-none-errors-in-the-stem-script-exit-used-py

As posted by @mirimir his code below essentially attaches a stream event listener function, which is then used to get the circuit id, circuit fingerprint, then finally the exit ip address -

#!/usr/bin/python

import functools
import time
from stem import StreamStatus
from stem.control import EventType, Controller

def main():
  print "Tracking requests for tor exits. Press 'enter' to end."
  print

  with Controller.from_port() as controller:
    controller.authenticate()

    stream_listener = functools.partial(stream_event, controller)
    controller.add_event_listener(stream_listener, EventType.STREAM)

    raw_input()  # wait for user to press enter

def stream_event(controller, event):
  if event.status == StreamStatus.SUCCEEDED and event.circ_id:
    circ = controller.get_circuit(event.circ_id)
    exit_fingerprint = circ.path[-1][0]
    exit_relay = controller.get_network_status(exit_fingerprint)
    t = time.localtime()

    print "datetime|%d-%02d-%02d %02d:%02d:%02d % (t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)
    print "website|%s" % (event.target)
    print "exitip|%s" % (exit_relay.address)
    print "exitport|%i" % (exit_relay.or_port)
    print "fingerprint|%s" % exit_relay.fingerprint
    print "nickname|%s" % exit_relay.nickname
    print "locale|%s" % controller.get_info("ip-to-country/%s" % exit_relay.address, 'unknown')
    print



回答2:


You can use this code for check current IP (change SOCKS_PORT value to yours):

import re

import stem.process
import requesocks

SOCKS_PORT = 9053

tor_process = stem.process.launch_tor()
proxy_address = 'socks5://127.0.0.1:{}'.format(SOCKS_PORT)
proxies = {
            'http': proxy_address,
            'https': proxy_address
        }
response = requesocks.get("http://httpbin.org/ip", proxies=proxies)
print re.findall(r'[\d.-]+', response.text)[0]
tor_process.kill()



回答3:


If you want to use socks you should do:

pip install requests[socks]

Then you can do:

import requests
import json
import stem.process
import stem


SOCKS_PORT = "9999"

tor = stem.process.launch_tor_with_config(
    config={
        'SocksPort': SOCKS_PORT,
    },
    tor_cmd= 'absolute_path/to/tor.exe',
)

r = requests.Session()

proxies = {
    'http': 'socks5://localhost:' + SOCKS_PORT,
    'https': 'socks5://localhost:' + SOCKS_PORT
}

response = r.get("http://httpbin.org/ip", proxies=proxies)
self.current_ip = response.json()['origin']


来源:https://stackoverflow.com/questions/32448750/how-to-get-the-tor-exitnode-ip-with-python-and-stem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!