TOR is extremely useful while you need to use python requests for without revealing the IP address, particularly when you do web scraping. This blog will utilize a wrapper for Python, which helps you similarly.
What is TOR?
To use TOR with Python, you will require two things
- A software installed in the computer to use TOR
- A network of different computers, which manages the TOR connections
In easy words, Python TOR web scraping helps you to track web traffic using numerous other computers with the intention that any third person won’t locate the traffic back to the user. Anybody who is trying to search the traffic might see random undetectable nodes about the TOR network.
How to Make Anonymous Requests using TOR Requests & Python?
TOR request Python uses TOR as anaddiction. So, you need to Install the TOR first.
These instructions are given for Debian or Ubuntu users. For installation on Mac or Windows, check here.
sudo apt-get update
sudo apt-get install TOR
Restart this TOR service
sudo /etc/init.d/TOR restart
Let’s provide a new password to provide random access to a port through outside agents are prevented.
TOR --hash-password (enter your password here)
You will find a longer combination of numbers and alphabets as your newer hashed password. Then go to the TOR configuration file (TORrc) to do the necessary changes
Where a TORrc file is located depends on an operating system you are using as well as where you are getting TOR from. The scrape was at ./etc/TOR/TORrc and you can mentionthat to understand more.
We can do three things here:
- Allow the “ControlPort” hearer for the TOR to listen port 9051 because it is a port for which TOR would listen for communications from applications chatting with a TOR controller.
- Update a hashed password
- Apply the cookie authentication
You can get this through uncommenting as well as editing the given lines just beyond the section of location unknown services.
SOCKSPort 9050 HashedControlPassword(your hashed password obtained earlier here)
### This section is just for location-hidden services ###
Save this and exit to restart TOR.
sudo /etc/init.d/TOR restart
Now your TOR is ready! Kudos!
What is TOR Request?
TOR Request is the wrapper around the requests as well as stem libraries, which allows in making the requests using TOR request Python. You can go through the project.
It’s easy to install TOR request through PyPI:
pip install TOR request
You can try TOR Request and open the python terminal.
Using TOR request import the TOR Request
Then pass the password to TOR
You can also check the current IP address
print ("My Original IP Address:",response.text)
Our response was
Our Original IP Address: 126.96.36.199
Let’s try itusing the TOR Request
tr.reset_identity() #Reset TOR
print ("New Ip Address",response.text)
You would get a diverse IP address. Reset the TOR again for getting the newer IP address yet again.
Now it’s easy to mask IP address with Python through TOR requests.
That’s it from us!