Multithreading requests via TOR with automatic TOR new identity
Project description
requests_tor
Multithreading requests via TOR with automatic TOR new identity.
Wrapper of the requests and stem libraries. Returns requests.Response object.
Masking as Tor Browser by using its default headers:
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.5",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0"
Install
pip install requests_tor
Dependencies
Download and start Tor Browser or install Tor
Simple usage
from requests_tor import RequestsTor
rt = RequestsTor() #for Tor Browser
rt = RequestsTor(tor_ports=(9050,), tor_cport=9051) #for Tor
url = 'https://foxnews.com'
r = rt.get(url)
print(r.text)
urls = (f'https://foxnews.com' for _ in range(10))
res = rt.get_urls(urls)
for result in res:
print(result.text)
Advanced usage
- add socks ports,
SocksPort 9000 IsolateDestAddr
SocksPort 9001 IsolateDestAddr
SocksPort 9002 IsolateDestAddr
SocksPort 9003 IsolateDestAddr
SocksPort 9004 IsolateDestAddr
- add password for control port [not necessary]:
generate and add in torrc file HashedControlPassword.
HashedControlPassword hashed_password
from requests_tor import RequestsTor
rt = RequestsTor(self, tor_ports=(9000, 9001, 9002, 9003, 9004), tor_cport=9151, password=None,
autochange_id=5, threads=8,)
"""
tor_ports = specify Tor socks ports tuple (default is (9150,), as the default in Tor Browser),
if more than one port is set, the requests will be sent sequentially through the each port;
tor_cport = specify Tor control port (default is 9151 for Tor Browser, for Tor use 9051);
password = specify Tor control port password (default is None);
autochange_id = number of requests via a one Tor socks port (default=5) to change TOR identity.
threads = specify threads to download urls list (default=8),
"""
# check your ip
rt.check_ip()
# new Tor identity. Сalling this function includes time.sleep(3)
rt.new_id()
# test automatic TOR new identity
rt.test()
# get url
url = 'https://foxnews.com'
r = rt.get(url)
print(r.text)
# get urls list concurrently. TOR new identity is executed depending on the number of socksports and
# autochange_id parameter. In case of 5 socksports and autochange_id=5, after downloading 5*5=25 urls
# TOR identity will be changed. It does matter, because calling TOR new identity includes time.sleep(3)
urls = (f'https://api.my-ip.io/ip' for _ in range(10))
results = rt.get_urls(urls)
for result in results:
print(result.url, result.text)
Example: downloading list of urls concurrently with unique ip for each url
Urls: https://habr.com/ru/post/1 - https://habr.com/ru/post/50
from requests_tor import RequestsTor
rt = RequestsTor(tor_ports=(9000, 9001, 9002, 9003, 9004), autochange_id=1)
urls = (f'https://habr.com/ru/post/{x}' for x in range(1, 50))
results = rt.get_urls(urls)
for result in results:
print(result.status_code, result.url)
print(results[-1].text)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
requests_tor-0.6.tar.gz
(4.3 kB
view hashes)
Built Distribution
Close
Hashes for requests_tor-0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c9a448b07903162ab9e749683e590e392495579abb3b8172c7011df047c8862 |
|
MD5 | 1e3eefe908e2e77b12ebefff806d6001 |
|
BLAKE2b-256 | cd97b6ef5a7376d5f82fc0a51de2e2493c28083e6f333ec2bddd97c45ddd99b1 |