In today's interconnected world, privacy and anonymity have become increasingly important concerns for individuals browsing the internet. Whether it's to protect sensitive information, bypass censorship, or maintain anonymity while accessing certain resources, utilizing tools like Tor can provide a robust solution. Tor, short for "The Onion Router," is a network of volunteer-operated servers that allows users to browse the internet anonymously by routing their traffic through a series of encrypted relays.
While Tor offers its own browser for anonymous browsing, there are times when we may want to incorporate Tor functionality directly into our Python applications. This is where the Stem library comes in. Stem is a Python library that provides a simple and powerful way to interact with the Tor network programmatically. With Stem, we can set up a Tor proxy within our Python code, enabling us to route requests through the Tor network, access onion services, and even manage Tor circuits for enhanced anonymity.
In this blog post, we will explore how to set up a Tor proxy using the Stem library in Python. We will guide you through the installation and configuration of Tor, demonstrate the steps to establish a connection to the Tor control port using Stem, and showcase how to make HTTP requests through the Tor proxy. Additionally, we will cover advanced topics such as circuit renewal and obtaining a new identity, empowering you to leverage the full potential of Tor within your Python applications.
Whether you are building web scrapers, privacy-focused applications, or conducting research that requires anonymity, understanding how to set up a Tor proxy in Python will give you the tools to protect your privacy and ensure secure communication over the internet.
Let's dive in and learn how to harness the power of Tor and Stem in Python!
Installing Tor:
To begin, we need to install Tor on our system. Depending on your Linux distribution, use the following commands:
Ubuntu:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install tor
Arch:
sudo pacman -Syu
sudo pacman -S tor
Once the installation is successful, we can proceed to configure Tor.
Configuring Tor:
- Open the Tor configuration file using a text editor
sudo nano /etc/tor/torrc
2. Uncomment the line containing ControllerPort
by removing the '#' symbol
ControllerPort 9050
3. Uncomment the line containing HashedControlPassword
by removing the '#' symbol and set the hashed password. Replace <hashed_password>
with the hashed value of the password you want to set
HashedControlPassword <hashed_password>
To obtain the hashed password, you can run the following command:
tor --hash-password your_password
Replace your_password
with the actual password you want to use. The command will output the hashed password, which you should copy and paste into the <hashed_password>
field
Save the changes and exit the text editor.
Restart the Tor service
sudo systemctl restart tor
Now that Tor is properly installed and configured, we can proceed to set up the Tor proxy in Python.
Python with Tor proxy:
Make sure you have python installed on your device.if not, run these commands on the terminal.
Ubuntu:
sudo apt-get install python3
sudo apt-get install python3-pip
Arch
sudo pacman -S python
sudo pacman -S python-pip
Now that the python installed. we can proceed to install required python libraries for our proxy project.
requests
stem
install the required the library using the pip command.
pip3 install library_name
Once the dependencies are installed, we can start integrating Tor proxy into our Python code. Below is a simple code snippet that demonstrates how to make an HTTP request using the Tor network:
from stem import Signal
from stem.control import Controller
import requests
def renew_identity():
with Controller.from_port(port=9050) as controller:
controller.authenticate(password="your_password")
controller.signal(Signal.NEWNYM)
proxies = {
'http': 'socks5h://localhost:9050',
'https': 'socks5h://localhost:9050'
}
response = requests.get('<https://api.ipify.org>',proxies=proxies)
print(f"1st Tor ip : {response.text}")
renew_identity()
response = requests.get('<https://api.ipify.org>',proxies=proxies)
print(f"2nd Tor ip : {response.text}")
The above code snippet sets up a Tor proxy with authentication using the "stem" library and demonstrates how to change the IP address by renewing the Tor identity.
First, the code defines the proxies
dictionary, specifying the proxy settings for both http and https protocols. It uses the SOCKS5 proxy located at "localhost" on port 9050.
To authenticate with Tor, the code includes the renew_identity()
function. This function establishes a connection with the Tor control port, authenticates using the provided password, and sends a signal to request a new identity from the Tor network.
The code then sends an HTTP GET request to <
https://api.ipify.org
>
, utilizing the requests.get()
function. The proxies
dictionary is passed to the proxies
parameter to route the request through the Tor proxy. The response text, which contains the IP address seen from the Tor network, is printed.
Afterwards, the renew_identity()
function is called to request a new Tor identity. This process authenticates with the Tor control port, signals for a fresh circuit, and changes the IP address used for subsequent requests.
Finally, another HTTP GET request is sent to <
https://api.ipify.org
>
using the updated Tor identity. The new IP address obtained from the response is printed.
By executing this code, you can observe the initial IP address obtained from the first request, followed by the IP address after renewing the circuit. This demonstrates how the IP address changes after requesting a new identity, highlighting the ability to achieve anonymity and privacy through the Tor network.
Unlocking Tor Proxy's Potential:
The ability to change IP addresses using Tor opens up a world of possibilities for various use cases. Let's explore some scenarios where this capability can be incredibly valuable:
Sneaky Web Scraping: Imagine you're building a price comparison website, but certain e-commerce platforms have implemented measures to prevent automated scraping. With Tor's IP-changing prowess, you can cleverly maneuver around these roadblocks. It's like having a secret identity that allows you to gather data from multiple sources discreetly, giving your website the edge it needs.
Ghost Mode Browsing: Have you ever wanted to browse the internet without leaving any traces behind? With Tor, you can become a digital ghost. By effortlessly changing your IP address, you can browse the web incognito. It's like wearing an invisibility cloak, shielding your online activities from prying eyes and trackers. Your privacy remains intact, and you can explore the online world with peace of mind.
World Traveler Access: Traveling is exciting, but it can come with frustrating geo-restrictions. Imagine being abroad and missing out on your favorite shows or content exclusive to your home country. Fear not! With Tor's IP-changing capabilities, you can virtually transport yourself back home, bypassing those pesky restrictions. It's like having a virtual passport that grants you access to the content you love, no matter where you are.
Evading IP Bans: It can be disheartening to find yourself banned from a website or online community. But with Tor, you have the power to overcome those bans. By switching IP addresses, you can regain access and rejoin the conversation. It's like a fresh start, enabling you to continue participating in the online communities that matter to you.
The ability to change IP addresses using Tor is a powerful tool that can enhance your online experiences, protect your privacy, and overcome obstacles. However, it's important to use this capability responsibly and in compliance with legal and ethical standards. Let's embrace the potential of Tor while being mindful of our digital footprint and respecting the rules of the online world.
Conclusion:
Tor Proxy serves as a powerful tool for enhancing online privacy, security, and accessibility. By leveraging its ability to change IP addresses and route traffic through the Tor network, users can protect their identity, bypass geographical restrictions, and unlock new possibilities in the digital realm. Whether it's safeguarding sensitive information, conducting anonymous research, or exploring the internet without borders, Tor Proxy empowers individuals to take control of their online experiences. Embrace the power of Tor Proxy and embrace a world of anonymity, security, and freedom.