Use Tor with Python (Latest code)

In this short, but direct tutorial, I am going to teach you, how to use TOR and Python together. After this tutorial, you will be able to visit TOR sites via your Python application.

Use Python and Tor together
Feel free to share this tutorial around

How to use TOR with Python

For this tutorial, we are going to need Python and PIP. Additionally, we need the TOR expert bundle.

Tor Windows Expert Bundle download
Screenshot of the Windows expert bundle page

Time needed: 5 minutes.

After this tutorial, you will be able to use TOR and Python together.

  1. Download and install TOR expert bundle on your system

    Download TOR from the official TOR website.Tor running

  2. Install Python requests

    We are going to need Python request. You can install Python request with the following commands:

    2.1. pip install requests
    2.2. pip install requests[socks]
    2.3. pip install requests[security]

Python code that works with TOR

The following Python code allows you to run TOR and Python together. With the requests library, we will perform the requests. Before you run the code, make sure that you have TOR running.

import requests

domain = "https://cyberwarzone.com"
#define your proxies
#the socks5h method allows the socks server to translate the #hostname. So make sure that you add 'socks5h'.
proxies = {
    'http': 'socks5h://127.0.0.1:9050',
    'https': 'socks5h://127.0.0.1:9050'
}
a = requests.get(domain.strip(), proxies=proxies).text
print(a)

When can you use this

There can be various reasons on why you want to utilize TOR and Python together. Python itself is just a programming language, if this works in Python, then most likely, the same approach, will also work in other programming languages.

use case 1: Crawler

You can use this code to create your own Python to TOR crawler. Simply create a list or dictionary with the URLs you want to visit, and command the tool to utilize your list.

myurls = ['url1','url2','url3','url4']
for url in myurls:
    #dosomething

use case 2: Forensic investigation

In some cases, you might only be interested in text. The code allows you to download complete pages, without you having to watch the visual content. This can be a great asset in some use-cases.

my_interest = ['keyword1','keyword2','keyword3','keyword4']
for keyword in my_interest:
    if keyword in a:
       #dosomething
Share this information