-
Notifications
You must be signed in to change notification settings - Fork 1
/
scraper-tor.py
104 lines (76 loc) · 2.57 KB
/
scraper-tor.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import socket
import socks
import stem.process
import urllib
import random
import time
class ScraperTor():
def __init__(self, socks_port = 7000, socks_address = '127.0.0.1', tor_config = {}):
self.socks_port = socks_port
self.socks_address = socks_address
self.tor_config = tor_config
self.tor_process = None
def set_socks_port(self, port):
""" Set the port for SOCKS
"""
self.socks_port = port
def set_socks_address(self, address):
""" Set the address for SOCKS
"""
self.socks_address = address
def set_tor_config(self, tor_config):
""" Set tor configuration
"""
self.tor_config = tor_config
def _getaddrinfo(*args):
""" Perfomrs DNS resolution
"""
return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]
def set_socks_proxy(self, address = None, port = None):
""" Set socks proxy
"""
port = port or self.socks_port
address = address or self.socks_address
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, address, port)
socket.socket = socks.socksocket
socket.getaddrinfo = self._getaddrinfo
def _tor_print(self, line):
""" Print tor bootstrap info
"""
if "Bootstrapped" in line:
print "%s" % (line)
def set_tor_config(self, config):
""" Set tor configuration
"""
self.tor_config = config
def start_tor_process(self):
""" Start a tor process
"""
self.tor_process = stem.process.launch_tor(init_msg_handler = self._tor_print)
def start_tor_process_with_config(self, config = None):
""" Start a tor process with configuration
"""
config = config or self.tor_config
if not config:
config = {
'SocksPort': str(self.socks_port)
}
self.tor_process = stem.process.launch_tor_with_config(config, init_msg_handler = self._tor_print)
def stop_tor_process(self):
""" Stop tor process
"""
self.tor_process.kill()
if __name__ == '__main__':
tor = ScraperTor()
tor.set_socks_proxy()
print('SOCKS Address: %s Port: %s' % (tor.socks_address, tor.socks_port))
print('Start tor process...')
tor.start_tor_process()
time.sleep(10)
print('Stop tor process...')
tor.stop_tor_process()
print('Start tor process with config...')
tor.start_tor_process_with_config()
time.sleep(10)
print('Stop tor process...')
tor.stop_tor_process()