paint-brush
How to Make Requests Over Tor Browser Using Pythonby@arthtyagi
2,818 reads
2,818 reads

How to Make Requests Over Tor Browser Using Python

by Arth TyagiJune 30th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

How to Make Requests Over Tor Browser Using Python3, we are going to be utilizing a Python controller library for Tor called Stem. In order to do so, you need to install the stem package for your OS. For MacOS: pip3 install stem in a python3 venv (recomm recommended) for MacOS : pip3. for Ubuntu: sudo pip3. install stem for Ubuntu or pip3 for Mac OSX. For macOS, it can be easily installed using the pip3 package.
featured image - How to Make Requests Over Tor Browser Using Python
Arth Tyagi HackerNoon profile picture

To make requests over Tor using Python3, we are going to be utilizing a Python controller library for Tor called Stem.

In order to do so, you need to install the stem package for your OS.

  • For MacOS :
    pip3 install stem
    in a python3 venv ( recommended ).
  • For Ubuntu:
    sudo pip3 install stem
    in a python3 venv ( recommended ).
  • from stem import Signal
    from stem.control import Controller
    with Controller.from_port(port = 9051) as controller:
        controller.authenticate(password='your password set for tor controller port in torrc')
        print("Success!")
        controller.signal(Signal.NEWNYM)
        print("New Tor connection processed")

However, you're not done yet. You might get this error :

error(61, 'Connection refused')

It's because Port 9051 is not open on your machine yet.

To fix this issue, you'll need to edit your torrc file and uncomment the corresponding lines.

ControlPort 9051
## If you enable the controlport, be sure to enable one of these
## authentication methods, to prevent attackers from accessing it.
HashedControlPassword16:<your_password>

To generate your password, run :

tor --hash-password "<new_password>"

where

<new_password>
is the password that you want to set.

And you're done with Making Requests over Tor.

Note: To run the

tor
commands, you need to have
tor
package installed. For macOS, it can be easily installed using
brew install tor
.

Previously published at https://dev.to/arthtyagi/how-to-make-requests-over-tor-58ep