How to make requests via tor in Python

Why Route HTTP Requests Through Tor?

Imagine you’re working on a web scraping project, and suddenly, your IP gets blocked. Or maybe you’re building a privacy-focused application where user anonymity is paramount. In both scenarios, Tor can be a game-changer. Tor (The Onion Router) is a network designed to anonymize internet traffic by routing it through multiple servers (or nodes), making it nearly impossible to trace the origin of a request.

But here’s the catch: using Tor isn’t as simple as flipping a switch. It requires careful setup and an understanding of how to integrate it with your Python code. In this guide, I’ll walk you through two approaches to making HTTP requests via Tor: using the requests library with a SOCKS5 proxy and leveraging the stem library for more advanced control.

🔐 Security Note: While Tor provides anonymity, it doesn’t encrypt your traffic beyond the Tor network. Always use HTTPS for secure communication.

Setting Up Tor on Your Machine

Before diving into the code, you need to ensure that Tor is installed and running on your machine. Here’s how you can do it:

  • Linux: Install Tor using your package manager (e.g., sudo apt install tor on Ubuntu). Start the service with sudo service tor start.
  • Mac: Use Homebrew: brew install tor, then start it with brew services start tor.
  • Windows: Download the Tor Expert Bundle from the official Tor Project website and run the Tor executable.

By default, Tor runs a SOCKS5 proxy on 127.0.0.1:9050. We’ll use this proxy to route our HTTP requests through the Tor network.

Method 1: Using the requests Library with a SOCKS5 Proxy

The simplest way to route your HTTP requests through Tor is by configuring the requests library to use Tor’s SOCKS5 proxy. Here’s how:

Step 1: Install Required Libraries

First, ensure you have the requests library installed. If not, install it using pip:

pip install requests[socks]

Step 2: Create a Tor Session

Next, create a function to configure a requests session to use the SOCKS5 proxy:

import requests

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

Notice the use of socks5h instead of socks5. The socks5h scheme ensures that DNS resolution is performed through the Tor network, adding an extra layer of privacy.

📚 Continue Reading

Sign in with your Google or Facebook account to read the full article.
It takes just 2 seconds!

Already have an account? Log in here

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *