Tor on Linux and Python URL Requests

In the age of increasing concerns about online privacy and data security, using the Tor network has become a popular choice for those looking to anonymize their internet activities. Tor, which stands for “The Onion Router,” enables anonymous communication by directing internet traffic through a free, worldwide, volunteer overlay network consisting of more than seven thousand relays. This blog post will guide you through the steps to install Tor on a Linux system and demonstrate how to make URL requests in Python through the Tor network.

Installing Tor on Linux

Step 1: Update Your System

Before installing any new software, it’s a good practice to update your system’s package list and upgrade all your installed packages. Open your terminal and run:

sudo apt update
sudo apt upgrade

Step 2: Install Tor

The Tor package is available in most Linux distribution repositories. You can install it using the package manager. For Debian-based distributions like Ubuntu, run:

sudo apt install tor

For Red Hat-based distributions like Fedora, use:

sudo dnf install tor

After installation, Tor should start automatically. You can check its status with:

systemctl status tor

To start Tor manually, use:

sudo systemctl start tor

And to enable it to start at boot:

sudo systemctl enable tor

Step 3: Verify Tor Installation

To verify that Tor is running correctly, you can use the following command:

tor --version

You should see the version number of Tor installed on your system.

Configuring Tor

By default, Tor runs on port 9050. This is the port you’ll use to configure your applications to route their traffic through the Tor network. No further configuration is necessary for basic use, but you can modify /etc/tor/torrc for advanced settings.

Making URL Requests in Python Through the Tor Network

To make URL requests in Python through the Tor network, you need to route your requests through Tor’s SOCKS proxy. This can be achieved using the requests library along with the requests[socks] dependency.

Step 1: Install Required Libraries

First, ensure you have requests and requests[socks] installed. You can install them using pip:

pip install requests[socks]

Step 2: Write the Python Script

Here’s a simple script to make a URL request through Tor:

import requests

proxies = {
    'http': 'socks5h://127.0.0.1:9050',
    'https': 'socks5h://127.0.0.1:9050'
}

url = 'http://httpbin.org/ip'

response = requests.get(url, proxies=proxies)
print(response.text)

In this script:

  • We define the proxies dictionary to route HTTP and HTTPS traffic through the Tor network running on 127.0.0.1:9050.
  • We make a GET request to http://httpbin.org/ip, a service that returns your IP address. This will show the IP address of the Tor exit node, not your actual IP.

Step 3: Run the Script

Execute the script:

python your_script_name.py

You should see output similar to:

{
  "origin": "TorExitNodeIP"
}

This confirms that your request was routed through the Tor network.

Conclusion

By following this guide, you’ve successfully installed Tor on your Linux system and learned how to make anonymous URL requests using Python. The Tor network is a powerful tool for protecting your online privacy, and integrating it with Python opens up a variety of applications, from web scraping to secure browsing. Remember to use these tools responsibly and in accordance with the law. Happy anonymizing!