HTTP request with requests and TOR - @kholidfu

Table of Contents

Disclaimer: OS yang digunakan dalam artikel ini adalah macos sierra

1 Install dan Setup TOR

1.1 Install tor di macos menggunakan brew

brew install tor

1.2 tor config

# copy on mac /usr/local/etc/tor/torrc.sample ke torrc
# on ubuntu, file ada di /etc/tor/torrc

# uncomment
ControlPort 9051
# and
CookieAuthentication 1
# change line above to
CookieAuthentication 0

2 python setup and code example

Buat virtual environment, kemudian install requests[socks] dan stem:

pip install requests[socks]
pip install stem
# code example :: app.py
import requests
import time

proxies = {
    'http': 'socks5://localhost:9050',
    'https': 'socks5://localhost:9050'
}
url = 'http://httpbin.org/ip'


# refresh IP
from stem import Signal
from stem.control import Controller

# signal TOR for a new connection 
def renew_connection():
    with Controller.from_port(port = 9051) as controller:
	controller.authenticate()
	controller.signal(Signal.NEWNYM)

for i in range(5):
    print(requests.get(url, proxies=proxies).text)
    renew_connection()
    time.sleep(5)

Sekarang jalankan tor

tor

3 Reference

Date: <2016-10-14 Fri>

Author: Kholid Fuadi

Created: 2016-10-14 Fri 08:20

Emacs 25.1.1 (Org mode 8.2.10)

Validate