Search code examples
pythonpython-requeststor

Making a request using requests.get through tor proxies but the code is not responding


I want to make multiple GET requests using Tor to a webpage. I want to use a different ipaddress for each request,So i write the small program

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

def change_ip():
    with Controller.from_port(port=9051) as contr:
        contr.authenticate(password='abhishek')
        contr.signal(Signal.NEWNYM)


session=requests.session()

session.proxies={}

session.proxies['http']='socks5://127.0.0.1:9051'
session.proxies['https']='socks5://127.0.0.1:9051'

for i in range(5):
    r=session.get('http://httpbin.org/ip')
    print(r.text)
    change_ip()

Using this, i made multiple requests but this program not show any output and it stuck like i have shown in this image this is the screenshot of terminal where i run this program and it stucked

but when i remove the session.proxies area of code, the code is running and shows the output but it makes no sense to me because i want to change ip address after each request.


Solution

  • Tor runs proxy on port 9050, not 9051. Port 9051 is used only to control/change Tor.


    I also need few seconds after sending singnal to get new IP.

    And it works better when I don't use one session for all urls but normal requests

    requests.get(..., proxies=proxies)
    

    With one session it sometimes gives the same IP for https://httpbin.org/ip and https://api.ipify.org but not for https://icanhazip.com .

    It works correctly if I create new session in every loop.


    Version without session

    from stem import Signal
    from stem.control import Controller
    import requests
    import time
    
    def change_ip():
        with Controller.from_port(port=9051) as control:
            control.authenticate(password='password')
            control.signal(Signal.NEWNYM)
    
    proxies = {
        'http':  'socks5://127.0.0.1:9050',
        'https': 'socks5://127.0.0.1:9050',
    }    
    
    for i in range(5):
    
        r = requests.get('https://httpbin.org/ip', proxies=proxies)
        print(r.json()['origin'])
        
        r = requests.get('https://api.ipify.org', proxies=proxies)
        print(r.text)
    
        r = requests.get('https://icanhazip.com', proxies=proxies)
        print(r.text)
    
        change_ip()
        time.sleep(5)
    

    Version with session - new session in every loop

    from stem import Signal
    from stem.control import Controller
    import requests
    import time
    
    def change_ip():
        with Controller.from_port(port=9051) as control:
            control.authenticate(password='password')
            control.signal(Signal.NEWNYM)
    
    for i in range(5):
    
        session = requests.Session()
        
        session.proxies = {
            'http':  'socks5://127.0.0.1:9050',
            'https': 'socks5://127.0.0.1:9050',
        }    
        
        r = session.get('https://httpbin.org/ip')
        print(r.json()['origin'])
    
        r = session.get('https://api.ipify.org')
        print(r.text)
    
        r = session.get('https://icanhazip.com')
        print(r.text)
    
        change_ip()
        time.sleep(5)