Knowledgebase / Self-hosting Guide / How can I use TOR as a proxy in Python?

How can I use TOR as a proxy in Python?

Tor, an acronym for The Onion Router, is an open-source software that is free to use and promotes anonymous interactions online. It routes internet traffic through a global volunteer-driven overlay network comprising over seven thousand relays. The use of Tor increases the challenge of tracing a user's online activities.

Python is a universal programming language that functions at a high-level. It is designed with a philosophy that prioritizes code readability and utilizes substantial indentation. It can also be deployed on a server for the development of web applications.

This guide outlines two common methods to utilize TOR as a proxy using Python language.

The initial method involves installing the Tor Browser, which can be downloaded from the official Tor website. The website provides instructions to download a version compatible with your operating system. After the download is complete, the next step is to install the browser on your device. The installation process is straightforward and similar to that of any other software or application.

Download Tor Browser for Windows

Download Tor Browser for macOS

Download Tor Browser for Android

The first time you launch the Tor Browser, connection to the Tor network must be established. This can be achieved by clicking the "Connect" button found on the browser's interface. This step is required only during the initial use of the Tor Browser. It is meant to ensure a secure and private connection to the Tor network.

The first time you launch the Tor Browser, connection to the Tor network must be established. This can be achieved by clicking the Connect button found on the browser's interface.

The first time you launch the Tor Browser, connection to the Tor network must be established. This can be achieved by clicking the Connect button found on the browser's interface.

The first time you launch the Tor Browser, connection to the Tor network must be established. This can be achieved by clicking the Connect button found on the browser's interface.

After the connection is established, you can use the Tor Browser as a proxy to connect to a URL. The Tor Browser opens the SOCKS proxy on 127.0.0.1:9150.

To use proxy in Python language,
requests
package is required in this scenario. This is the package which is a module that send HTTP/1.1 requests extremely easily.
The below commands can be executed to install the
requests
package.
// For Python2
pip install -U 'requests[socks]'
// For Python3
pip3 install -U 'requests[socks]'

Below script is written in Python language to show you how to use tor as a proxy.

import requests

# resp = requests.get('http://2gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid.onion', proxies=dict(http='socks5h://127.0.0.1:9150', https='socks5h://127.0.0.1:9150'))
resp = requests.get('https://api.ipify.org', proxies=dict(http='socks5h://127.0.0.1:9150', https='socks5h://127.0.0.1:9150'))

print(resp.text)

When you execute this script using Python language, it will show the IP address of your Tor instance exit node, instead of your IP address. This is the result of using Tor as a proxy in Python language.

The second method introduced is by utilizing Docker. Docker is a software platform designed to facilitate building, testing, and deploying applications with speed. It organizes software into uniform units known as containers, which incorporate all the necessary components for the software's operation, such as libraries, system tools, code, and runtime. Docker enables swift deployment and scaling of applications in any environment, ensuring the smooth running of your code.

To utilize Docker to run Tor instance, execute the below command:

$ docker run --rm -p 9050:9050 osminogin/tor-simple

Mar 27 01:36:09.530 [notice] Tor 0.4.8.10 running on Linux with Libevent 2.1.12-stable, OpenSSL 3.1.2, Zlib 1.3, Liblzma 5.4.5, Libzstd 1.5.5 and Unknown N/A as libc.
Mar 27 01:36:09.530 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://support.torproject.org/faq/staying-anonymous/
Mar 27 01:36:09.530 [notice] Read configuration file "/etc/tor/torrc".
Mar 27 01:36:09.533 [warn] You specified a public address '0.0.0.0:9050' for SocksPort. Other people on the Internet might find your computer and use it as an open proxy. Please don't allow this unless you have a good reason.
Mar 27 01:36:09.533 [notice] Opening Socks listener on 0.0.0.0:9050
Mar 27 01:36:09.534 [notice] Opened Socks listener connection (ready) on 0.0.0.0:9050
Mar 27 01:36:09.534 [warn] Fixing permissions on directory /var/lib/tor

The docker run command runs a command in a new container, pulling the image if needed and starting the container. In this example, it pulls a public Tor image from the Docker Hub registry. Use --rm flag to specify that the container should be removed when it exits. Use -p flag to publish a container port 9050 to the host.

Then, you can try to execute the below Python script. This script shows how to use tor as a proxy using Python language.

import requests

# resp = requests.get('http://2gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid.onion', proxies=dict(http='socks5h://127.0.0.1:9050', https='socks5h://127.0.0.1:9050'))
resp = requests.get('https://api.ipify.org', proxies=dict(http='socks5h://127.0.0.1:9050', https='socks5h://127.0.0.1:9050'))

print(resp.text)

Note that when you run Tor instance using Docker, the SOCKS port is 9050, which differs than the SOCKS port used in Tor Browser. This is to ensure that there is no conflict when your system runs Tor instance and Tor Browser at the same time.

This guide demonstrates two common ways of how to use TOR as a proxy using Python language.

Link to Tor Project

Link to Docker

Link to Docker Compose

Link to Python

Tags:
  • Tor
  • Tor Browser
  • SOCKS proxy
  • Docker
  • Python

24/7 Expert Support

Our experts are always on hand to help answer your questions, get you started, and grow your presence online. You can email us any time