Menu Close

How to make requests via tor in Python

First make sure tor is running, with below code, you will construct a proxy session for tor.

import requests

def get_tor_session():
    session = requests.session()
    session.proxies = {'http':  'socks5://127.0.0.1:9050', 'https': 'socks5://127.0.0.1:9050'}
    return session

Then you can test your session via:

session = get_tor_session()
print('Tor IP')
print(session.get("http://httpbin.org/ip").text)

Leave a Reply

Your email address will not be published.