How to Make HTTP Requests Through Tor with Python

Updated Last updated: May 1, 2026 · Originally published: May 12, 2022

Why Use Tor for HTTP Requests?

πŸ“Œ TL;DR: 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. Or perhaps you’re working on a privacy-first application where user anonymity is non-negotiable.
🎯 Quick Answer: Route Python HTTP requests through Tor by installing the requests and PySocks libraries, then configuring a SOCKS5 proxy at 127.0.0.1:9050: use requests.get(url, proxies={‘http’: ‘socks5h://127.0.0.1:9050’, ‘https’: ‘socks5h://127.0.0.1:9050’}). The ‘h’ suffix routes DNS through Tor too.

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.

πŸ” Security Note: Tor anonymizes your traffic but does not encrypt it beyond the Tor network. Always use HTTPS to protect the data you send and receive.

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 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, extract it, and run the tor.exe executable.

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.

Pro Tip: After installing Tor, verify that it’s running by checking if the port 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.

Warning: If you receive errors or no response, double-check that the Tor service is running and listening on 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 torrc file for the correct hashed password and restart Tor after modifications.

Quick Summary

  • Tor enhances anonymity by routing traffic through multiple nodes.
  • The requests library with a SOCKS5 proxy is simple and effective for basic use cases.
  • The stem library provides advanced control, including dynamic IP changes.
  • Always use HTTPS to secure your data, even when using Tor.
  • Troubleshooting tools like netstat and careful torrc configuration can resolve most issues.
🛠 Recommended Resources:

Tools and books mentioned in (or relevant to) this article:

📋 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

πŸ“Š Free AI Market Intelligence

Join Alpha Signal β€” AI-powered market research delivered daily. Narrative detection, geopolitical risk scoring, sector rotation analysis.

Join Free on Telegram β†’

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.

Subscribe Free →

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

πŸ“§ Get weekly insights on security, trading, and tech. No spam, unsubscribe anytime.

Also by us: StartCaaS — AI Company OS · Hype2You — AI Tech Trends