Why Use Tor for HTTP Requests?
When your IP gets blacklisted mid-scrape or you need to test geo-restricted endpoints, routing HTTP requests through Tor from Python is the fastest path to a working proxy. The requests library plus a local Tor SOCKS proxy gives you rotating exit nodes with minimal setup.
Tor is not just a tool for bypassing restrictions; itβs a cornerstone of privacy on the internet. From journalists working in oppressive regimes to developers building secure applications, Tor is widely used for anonymity and bypassing censorship. It allows you to mask your IP address, avoid surveillance, and access region-restricted content.
However, integrating Tor into your Python projects isn’t as straightforward as flipping a switch. It requires careful configuration and a solid understanding of the tools involved. Today, I’ll guide you through two solid methods to make HTTP requests via Tor: using the requests library with a SOCKS5 proxy and Using the stem library for advanced control. By the end, you’ll have all the tools you need to bring the power of Tor into your Python workflows.
Getting Tor Up and Running
Before we dive into Python code, we need to ensure that Tor is installed and running on your system. Here’s a quick rundown for different platforms:
- Linux: Install Tor via your package manager, e.g.,
sudo apt install tor. Start the service withsudo service tor start. - Mac: Use Homebrew:
brew install tor. Then start it withbrew services start tor. - Windows: Download the Tor Expert Bundle from the official Tor Project website, extract it, and run the
tor.exeexecutable.
By default, Tor runs a SOCKS5 proxy on 127.0.0.1:9050. This is the endpoint we’ll use to route HTTP requests through the Tor network.
9050 is active. On Linux/Mac, use netstat -an | grep 9050. On Windows, use netstat -an | findstr 9050.Method 1: Using the requests Library with a SOCKS5 Proxy
The simplest way to integrate Tor into your Python project is by configuring the requests library to use Tor’s SOCKS5 proxy. This approach is lightweight and straightforward but offers limited control over Tor’s features.
Step 1: Install Required Libraries
First, ensure you have the necessary dependencies installed. The requests library needs an additional component for SOCKS support:
pip install requests[socks]
Step 2: Configure a Tor-Enabled Session
Create a reusable function to configure a requests session that routes traffic through Tor:
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
The socks5h protocol ensures that DNS lookups are performed through Tor, adding an extra layer of privacy.
Step 3: Test the Tor Connection
Verify that your HTTP requests are being routed through the Tor network by checking your outbound IP address:
session = get_tor_session()
response = session.get("http://httpbin.org/ip")
print("Tor IP:", response.json())
If everything is configured correctly, the IP address returned will differ from your machine’s regular IP address. This ensures that your request was routed through the Tor network.
127.0.0.1:9050. Troubleshooting steps include restarting the Tor service and verifying your proxy settings.Method 2: Using the stem Library for Advanced Tor Control
If you need more control over Tor’s capabilities, such as programmatically changing your IP address, the stem library is your go-to tool. It allows you to interact directly with the Tor process through its control port.
Step 1: Install the stem Library
Install the stem library using pip:
pip install stem
Step 2: Configure the Tor Control Port
To use stem, you’ll need to enable the Tor control port (default: 9051) and set a control password. Edit your Tor configuration file (usually /etc/tor/torrc or torrc in the Tor bundle directory) and add:
ControlPort 9051
HashedControlPassword <hashed_password>
Generate a hashed password using the tor --hash-password command and paste it into the configuration file. Restart Tor for the changes to take effect.
Step 3: Interact with the Tor Controller
Use stem to authenticate and send commands to the Tor control port:
from stem.control import Controller
with Controller.from_port(port=9051) as controller:
controller.authenticate(password='your_password')
print("Connected to Tor controller")
Step 4: Programmatically Change Your IP Address
One of the most powerful features of stem is the ability to request a new Tor circuit (and thus a new IP address) with the SIGNAL NEWNYM command:
from stem import Signal
from stem.control import Controller
with Controller.from_port(port=9051) as controller:
controller.authenticate(password='your_password')
controller.signal(Signal.NEWNYM)
print("Requested a new Tor identity")
Step 5: Combine stem with HTTP Requests
You can marry the control capabilities of stem with the HTTP functionality of the requests library:
import requests
from stem import Signal
from stem.control import Controller
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
with Controller.from_port(port=9051) as controller:
controller.authenticate(password='your_password')
controller.signal(Signal.NEWNYM)
session = get_tor_session()
response = session.get("http://httpbin.org/ip")
print("New Tor IP:", response.json())
Troubleshooting Common Issues
- Tor not running: Ensure the Tor service is active. Restart it if necessary.
- Connection refused: Verify that the control port (
9051) or SOCKS5 proxy (9050) is correctly configured. - Authentication errors: Double-check your
torrcfile for the correct hashed password and restart Tor after modifications.
Quick Summary
- Tor enhances anonymity by routing traffic through multiple nodes.
- The
requestslibrary with a SOCKS5 proxy is simple and effective for basic use cases. - The
stemlibrary provides advanced control, including dynamic IP changes. - Always use HTTPS to secure your data, even when using Tor.
- Troubleshooting tools like
netstatand carefultorrcconfiguration can resolve most issues.
Tools and books mentioned in (or relevant to) this article:
- TP-Link 5-Port 2.5G Switch — 5-port 2.5GbE unmanaged switch ($100-120)
- Ubiquiti U6+ WiFi 6 Access Point — WiFi 6 access point ($99)
- Cat8 Ethernet Cable 20ft — Shielded patch cables ($12)
📋 Disclosure: Some links are affiliate links. If you purchase through these links, I earn a small commission at no extra cost to you. I only recommend products I have personally used or thoroughly evaluated.
π Related Articles
- Home Network Segmentation with OPNsense: A Complete Guide
- Developer’s Ultimate Hardware Guide for 2026: Build Your Perfect Setup
- Ultimate Guide to Secure Remote Access for Your Homelab
π Free AI Market Intelligence
Join Alpha Signal β AI-powered market research delivered daily. Narrative detection, geopolitical risk scoring, sector rotation analysis.
Pro with stock conviction scores: $5/mo
Get Weekly Security & DevOps Insights
Join 500+ engineers getting actionable tutorials on Kubernetes security, homelab builds, and trading automation. No spam, unsubscribe anytime.
Delivered every Tuesday. Read by engineers at Google, AWS, and startups.
Frequently Asked Questions
What is How to Make HTTP Requests Through Tor with Python about?
Why Use Tor for HTTP Requests? Picture this: youβre in the middle of a data scraping project, and suddenly, your IP address is blacklisted.
Who should read this article about How to Make HTTP Requests Through Tor with Python?
Anyone interested in learning about How to Make HTTP Requests Through Tor with Python and related topics will find this article useful.
What are the key takeaways from How to Make HTTP Requests Through Tor with Python?
Or perhaps youβre working on a privacy-first application where user anonymity is non-negotiable. Tor (The Onion Router) is the perfect solution for both scenarios. It routes your internet traffic thro
References
- Tor Project Documentation β Official documentation for the Tor anonymity network.
- Requests Library β Python β HTTP library for Python used as the foundation for Tor-proxied requests.
- Stem β Tor Controller Library for Python β Python library for interacting with the Tor network programmatically.
- Tor and HTTPS β EFF β Electronic Frontier Foundation guide to how Tor and HTTPS protect privacy.
π§ Get weekly insights on security, trading, and tech. No spam, unsubscribe anytime.
