-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.py
56 lines (42 loc) · 1.82 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import time
from urllib.request import ProxyHandler, build_opener, install_opener, Request, urlopen
from stem import Signal
from stem.control import Controller
class TorHandler:
def __init__(self):
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'}
def open_url(self, url):
# communicate with TOR via a local proxy (privoxy)
def _set_url_proxy():
proxy_support = ProxyHandler({'http': '127.0.0.1:8118'})
opener = build_opener(proxy_support)
install_opener(opener)
_set_url_proxy()
request = Request(url, None, self.headers)
return urlopen(request).read().decode('utf-8')
@staticmethod
def renew_connection():
with Controller.from_port(port=9051) as controller:
controller.authenticate(password='btt')
controller.signal(Signal.NEWNYM)
controller.close()
if __name__ == '__main__':
wait_time = 2
number_of_ip_rotations = 3
tor_handler = TorHandler()
ip = tor_handler.open_url('http://icanhazip.com/')
print('My first IP: {}'.format(ip))
# Cycle through the specified number of IP addresses via TOR
for i in range(0, number_of_ip_rotations):
old_ip = ip
seconds = 0
tor_handler.renew_connection()
# Loop until the 'new' IP address is different than the 'old' IP address,
# It may take the TOR network some time to effect a different IP address
while ip == old_ip:
time.sleep(wait_time)
seconds += wait_time
print('{} seconds elapsed awaiting a different IP address.'.format(seconds))
ip = tor_handler.open_url('http://icanhazip.com/')
print('My new IP: {}'.format(ip))