torrequest icon indicating copy to clipboard operation
torrequest copied to clipboard

How to reset IP?

Open ghost opened this issue 5 years ago • 5 comments

'tr.reset_identity' does not work. What exactly needs to be configured for it to work?

`from torrequest import TorRequest

with TorRequest(proxy_port=9050, ctrl_port=9051, password=None) as tr: response = tr.get('http://ipecho.net/plain') print(response.text) # not your IP address

tr.reset_identity()

response = tr.get('http://ipecho.net/plain') print(response.text) # another IP address, not yours input('')`

ghost avatar Dec 15 '18 02:12 ghost

I think you have to create a new TorRequest instance every time?

  1. If I don't create a new instance:
with TorRequest(password='Risk1234') as tr:

    response= requests.get('http://ipecho.net/plain')
    print ("My Original IP Address:",response.text)

    tr.reset_identity() #Reset Tor
    response= tr.get('http://ipecho.net/plain')
    print ("New Ip Address",response.text)

# with TorRequest(password='Risk1234') as tr:

    tr.reset_identity() #Reset Tor
    response= tr.get('http://ipecho.net/plain')
    print ("New Ip Address",response.text)

It gives

My Original IP Address: 174.xx.xx.xx New Ip Address 207.244.70.XX New Ip Address 207.244.70.XX

  1. But if I create a new TorRequest instance:
with TorRequest(password='Risk1234') as tr:

    response= requests.get('http://ipecho.net/plain')
    print ("My Original IP Address:",response.text)

    tr.reset_identity() #Reset Tor
    response= tr.get('http://ipecho.net/plain')
    print ("New Ip Address",response.text)

with TorRequest(password='Risk1234') as tr:

    tr.reset_identity() #Reset Tor
    response= tr.get('http://ipecho.net/plain')
    print ("New Ip Address",response.text)

It works properly:

My Original IP Address: 174.xx.xx.xx New Ip Address 104.244.76.xx New Ip Address 103.234.220.xxx

  1. If I don't reset identity but create a new instance:
with TorRequest(password='Risk1234') as tr:

    response= requests.get('http://ipecho.net/plain')
    print ("My Original IP Address:",response.text)

#     tr.reset_identity() #Reset Tor
    response= tr.get('http://ipecho.net/plain')
    print ("New Ip Address",response.text)

with TorRequest(password='Risk1234') as tr:

#     tr.reset_identity() #Reset Tor
    response= tr.get('http://ipecho.net/plain')
    print ("New Ip Address",response.text)

It doesn't reset:

My Original IP Address: 174.xx.xx.xx New Ip Address 104.244.76.xx New Ip Address 104.244.76.xx

hangyao avatar Jan 04 '19 23:01 hangyao

Perhaps reset_identity should be called in __enter__.

It feels like there ought to be a method that would expose this e.g. reset_ip ?

hayd avatar Mar 23 '19 16:03 hayd

Why IP doesn't change when I request it with this site "https://ipecho.net/extra", which is just a more detailed description of your IP in the same domain?

I used the 2nd approach also.

KangboLu avatar Sep 25 '19 17:09 KangboLu

I am usign the following to reset IP:

    tor.close()
    tor.__init__()

jcgoette avatar Dec 04 '19 17:12 jcgoette

@erdiaker Find a solution, guys!!! Just need to renew session after ip renewal.

just add new function to the class to renew our session

        self.session = requests.Session()
        self.session.proxies.update({
            'http': 'socks5://localhost:%d' % self.proxy_port,
            'https': 'socks5://localhost:%d' % self.proxy_port,
        })

and you need to call this function during IP renewal in reser_identity function

        self._reset_session()
        self.reset_identity_async()
        time.sleep(self.ctrl.get_newnym_wait())

VitaliiVlo avatar Jul 20 '20 11:07 VitaliiVlo