2

Why is Signal.NEWNYM not generating a new IP address?

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

with Controller.from_port(port=9051) as controller:
    print('Ready')
    controller.authenticate(password='872860B76453A77D60CA2BB8C1A7042072093276A3D701AD684053EC4C')
    print("Success!")
    controller.signal(Signal.NEWNYM)
    print("New Tor connection processed")
    print(requests.get('http://icanhazip.com').content)
randomir
  • 17,989
  • 1
  • 40
  • 55
Rajat Garg
  • 33
  • 7

2 Answers2

1

The NEWNYM signal is not meant for acquiring a new exit node, it merely marks the current circuit as dirty and ensures the new connections will use a new circuit.

From the stem docs:

An important thing to note is that a new circuit does not necessarily mean a new IP address. Paths are randomly selected based on heuristics like speed and stability. There are only so many large exits in the Tor network, so it's not uncommon to reuse an exit you have had previously.

Tor does not have a method for cycling your IP address. This is on purpose, and done for a couple reasons. The first is that this capability is usually requested for not-so-nice reasons such as ban evasion or SEO. Second, repeated circuit creation puts a very high load on the Tor network, so please don't!

Edit. I missed initially you're not using Tor (proxy) when making a request! That's the first part of the problem.

You should first install socks support for requests library (pip install requests[socks]) and then make a request via local Tor proxy, like this:

requests.get('https://httpbin.org/ip', 
             proxies={'http': 'socks5://127.0.0.1:9050',
                      'https': 'socks5://127.0.0.1:9050'}).text
Community
  • 1
  • 1
randomir
  • 17,989
  • 1
  • 40
  • 55
  • Actually, I am trying to parse google scholar for my research purpose and google is blocking me just after one request. I found change of IP solution and tried but it's showing my original IP not even tor used by this program. – Rajat Garg Jul 13 '17 at 23:45
  • I'm afraid `tor` is not going to help you for that purpose. – randomir Jul 13 '17 at 23:46
  • [Look here](https://stackoverflow.com/questions/9887505/how-to-change-tor-identity-in-python) I read some comments and suggestions from here to change ip after seconds. – Rajat Garg Jul 13 '17 at 23:55
  • Thank you so much for the help. Now I am getting new IP every time. Can I use it to make request to google and other websites (for crawling) like different user agent? – Rajat Garg Jul 14 '17 at 01:55
  • I am trying to modify my code and when I am using [this](http://icanhazip.com/) website for my Ip, it's showing same ip every time and showing captcha error. – Rajat Garg Jul 14 '17 at 02:00
  • There's nothing special about that site. I've tried, it works just fine via Tor. It's possible (probably) that it has some kind of request rate limiting per IP address, and since many people are using Tor (which has only about 1000 exit nodes), it's blocking your IP with captcha. – randomir Jul 14 '17 at 09:11
  • do u have any suggestions about how to crawl without get blocked? – Rajat Garg Jul 14 '17 at 18:56
  • That's not really a question for StackOverflow, probably more appropriate for [WebApps](https://webapps.stackexchange.com/). Besides, it's out of this question's scope. If you found my answer helpful please see [what to do when someone answers your question](https://stackoverflow.com/help/someone-answers). – randomir Jul 14 '17 at 19:02
1

When ever you make request you got the same IP or First time IP is not changing?

If same IP on requests: You should put script to sleep for 30 seconds and will work.

I had the same issue and resolved by sleep.

def renew_connection():
    with Controller.from_port(port=9051) as controller:
        controller.authenticate(password="YourPass")
        controller.signal(Signal.NEWNYM)
    time.sleep(30)

Also make sure Password and Port are correct!