如何使用TOR和Stem和python设置新身份?就像(tor浏览器)新身份一样

时间:2019-07-09 18:51:45

标签: python python-3.x signals tor stem

我无法理解Tor浏览器或python中新身份之间的区别吗?

Python: 我请求使用tor从python调用网站,但网站已达到限制,需要reCAPTCHA,因此我以新身份重设所有信息以跳过reCAPTCHA,但不起作用,并且IP更改成功

Tor浏览器: 但是这里的重点是当我使用Tor浏览器和网站显示reCAPTCHA和 我使Tor浏览器的新身份成功运行,并跳过了reCAPTCHA并且网站运行正常

from stem import Signal
from stem.control import Controller
import requests


proxies = {
    'http': 'socks5://127.0.0.1:9050',
    'https': 'socks5://127.0.0.1:9050'
}

def new_identity():
  with Controller.from_port(port = 9051) as controller:
      controller.authenticate()
      controller.signal(Signal.NEWNYM)


url = 'https://ifconfig.me/ip'
response = requests.get(url, proxies=proxies)
print('tor ip: {}'.format(response.text.strip()))

new_identity()

url = 'https://ifconfig.me/ip'
response = requests.get(url, proxies=proxies)
print('tor ip: {}'.format(response.text.strip()))

2 个答案:

答案 0 :(得分:1)

更好:

def renew_connection():
    with Controller.from_port(port=9051) as controller:
        controller.authenticate(password='password')
        controller.signal(Signal.NEWNYM)
        controller.close()
 
def request_tor(url, headers):
    renew_connection()
    session = requests.session()
    session.proxies = {}
    session.proxies['http'] = 'socks5h://localhost:9050'
    print((session.get(url)).text)

答案 1 :(得分:0)


    from torrequest import TorRequest
    from stem import Signal
    from stem.control import Controller
    import requests

    tr=TorRequest(password='your hash-password Here')

    url="https://ifconfig.me/ip"        
    response = tr.get(url)
    print('tor ip: {}'.format(response.text.strip()))
    tr.reset_identity() #Reset Tor
    response = tr.get(url)
    print('tor ip: {}'.format(response.text.strip()))