Use Tor to connect from urllib2 [Python] [Mac]

What is Tor

Tor can spoof its IP address to another address. This is very well done and has been abused for crimes. Sadly, the Japanese police can almost certainly not find the criminal if the only clue is the IP address. Criminals who have been using Tor these days have often been arrested, but they probably found clues outside of their IP addresses.

Install Tor

Mac people You can install it from HomeBrew.

brew install tor

HomeBrew installation command


usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Start Tor

tor

If you can start 100%, go to the terminal

Xxx XX XX:XX:XX.XXX [notice] Bootstrapped 100%: Done

Is displayed (X is a letter or number)

Use Tor from Python.

Once started, Tor can be used by connecting from socks: localhost: 9050. In Python, PySocks is used because it cannot be specified only with urllib2. The installation command is

pip install PySocks
# or
sudo easy_install PySocks

Code when used from Python

import urllib2, socket, socks

class Tor:
	def __init__(self):
		socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', 9050)
		socket.socket = socks.socksocket
	def test(self):
		return urllib2.urlopen("https://api.ipify.org?format=json").read()
		
if __name__ == "__main__":
	Tor = Tor()
	ip = Tor.test()
	print ip #The IP address via Tor is displayed.

Recommended Posts

Use Tor to connect from urllib2 [Python] [Mac]
Connect to sqlite from python
Connect to utf8mb4 database from python
Update Python on Mac from 2 to 3
I want to use jar from python
Connect to coincheck's Websocket API from Python
Use thingsspeak from python
Use fluentd from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Study from Python Hour7: How to use classes
Use MySQL from Python
[Bash] Use here-documents to get python power from bash
Connect to postgreSQL from Python and use stored procedures in a loop.
Use MySQL from Python
Use BigQuery from python.
Connect python to mysql
Python: Use zipfile to unzip from standard input
Use mecab-ipadic-neologd from python
Preferences to generate animated GIFs from Python on Mac
Connect to BigQuery with Python
Post from Python to Slack
Cheating from PHP to Python
[Python] How to use list 1
Connect to Wikipedia with Python
Use MySQL from Anaconda (python)
How to use Python argparse
Update python on Mac to 3.7-> 3.8
Python: How to use pydub
[Python] How to use checkio
Switch from python2.7 to python3.6 (centos7)
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
Use e-Stat API from Python
How to use Python bytes
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
Python: How to use async with
Use Stanford Core NLP from Python
Create folders from '01' to '12' with python
How to use Requests (Python Library)
How to use SQLite in Python
[Lambda] [Python] Post to Twitter from Lambda!
Read and use Python files from Python
Forcibly use Google Translate from python
[Introduction to Python] Let's use pandas
Python (from first time to execution)
Use kabu StationĀ® API from Python
View Mac desktop notifications from Python
Post images from Python to Tumblr
[Python] How to use list 3 Added
How to use OpenPose's Python API
Use Azure Blob Storage from Python
Use the Flickr API from Python
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
Easy to use Jupyter notebook (Python3.5)
Ssh connect to GCP from Windows