Remove tor request and switch to standalone requests

master
trivernis 5 years ago
parent 8269c78f9a
commit 23aa5bd137

@ -7,8 +7,8 @@ verify_ssl = true
[packages] [packages]
requests = "*" requests = "*"
torrequest = "*"
fake-useragent = "*" fake-useragent = "*"
stem = "*"
[requires] [requires]
python_version = "3.8" python_version = "3.8"

18
Pipfile.lock generated

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "06db23fe7606fa26a31e3b7af11b3de2e5245c2abbbd74db4e986e8ff2595ea7" "sha256": "1b9a80175da06b66acee213480ffc51f4c7c2566c55ed79e717e786b4653e26b"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
@ -44,14 +44,6 @@
], ],
"version": "==2.9" "version": "==2.9"
}, },
"pysocks": {
"hashes": [
"sha256:08e69f092cc6dbe92a0fdd16eeb9b9ffbc13cadfe5ca4c7bd92ffb078b293299",
"sha256:2725bd0a9925919b9b51739eea5f9e2bae91e83288108a9ad338b2e3a4435ee5",
"sha256:3f8804571ebe159c380ac6de37643bb4685970655d3bba243530d6558b799aa0"
],
"version": "==1.7.1"
},
"requests": { "requests": {
"hashes": [ "hashes": [
"sha256:43999036bfa82904b6af1d99e4882b560e5e2c68e5c4b0aa03b655f3d7d73fee", "sha256:43999036bfa82904b6af1d99e4882b560e5e2c68e5c4b0aa03b655f3d7d73fee",
@ -64,14 +56,8 @@
"hashes": [ "hashes": [
"sha256:a0b48ea6224e95f22aa34c0bc3415f0eb4667ddeae3dfb5e32a6920c185568c2" "sha256:a0b48ea6224e95f22aa34c0bc3415f0eb4667ddeae3dfb5e32a6920c185568c2"
], ],
"version": "==1.8.0"
},
"torrequest": {
"hashes": [
"sha256:3745d4ea3ffda98d7a034363c787adb37aab77bdab40094a4d937392cd4dae82"
],
"index": "pypi", "index": "pypi",
"version": "==0.1.0" "version": "==1.8.0"
}, },
"urllib3": { "urllib3": {
"hashes": [ "hashes": [

@ -1,13 +1,16 @@
import requests import requests
from torrequest import TorRequest import stem
import time
import random
from fake_useragent import UserAgent from fake_useragent import UserAgent
from stem.control import Controller
class Client: class Client:
def __init__(self): def __init__(self, session=None):
self.session = requests.Session() self.proxies = None
self.session.headers['UserAgent'] = UserAgent().random self.headers = {'UserAgent': UserAgent().random}
def request(self, *args, method='GET', **kwargs): def request(self, *args, method='GET', **kwargs):
if method == 'GET': if method == 'GET':
@ -24,26 +27,26 @@ class Client:
raise Exception('Invalid HTTP method specified!') raise Exception('Invalid HTTP method specified!')
def get(self, *args, **kwargs): def get(self, *args, **kwargs):
return self.session.get(*args, **kwargs) return requests.get(*args, proxies=self.proxies, headers=self.headers, **kwargs)
def post(self, *args, **kwargs): def post(self, *args, **kwargs):
return self.session.get(*args, **kwargs) return requests.get(*args, proxies=self.proxies, headers=self.headers, **kwargs)
def put(self, *args, **kwargs): def put(self, *args, **kwargs):
return self.session.put(*args, **kwargs) return requests.put(*args, proxies=self.proxies, headers=self.headers, **kwargs)
def patch(self, *args, **kwargs): def patch(self, *args, **kwargs):
return self.session.patch(*args, **kwargs) return requests.patch(*args, proxies=self.proxies, headers=self.headers, **kwargs)
def delete(self,*args, **kwargs): def delete(self, *args, **kwargs):
return self.session.delete(*args, **kwargs) return requests.delete(*args, proxies=self.proxies, headers=self.headers, **kwargs)
def close(self): def close(self):
self.session.close() self.headers = None
self.proxies = None
def reset(self): def reset(self):
self.session.headers['UserAgent'] = UserAgent().random self.headers['UserAgent'] = UserAgent().random
self.session.cookies.clear()
def __enter__(self): def __enter__(self):
return self return self
@ -54,17 +57,30 @@ class Client:
class TorClient(Client): class TorClient(Client):
def __init__(self, *args, **kwargs): def __init__(self, proxy_port=9050, ctrl_port=9051, password=None):
self.proxy_port = proxy_port
self.ctrl_port = ctrl_port
super().__init__() super().__init__()
self.tr = TorRequest(*args, **kwargs) self.ctrl = Controller.from_port(port=self.ctrl_port)
self.tr.session.headers['UserAgent'] = UserAgent().random self.ctrl.authenticate(password=password)
self.session = self.tr self.ip_retrieval_sites = [
'http://ipecho.net/plain',
'https://ident.me',
]
self.proxies = {
'http': 'socks5://127.0.0.1:%d' % self.proxy_port,
'https': 'socks5://127.0.0.1:%d' % self.proxy_port
}
def new_identity(self): def new_identity(self):
self.tr.reset_identity() print('[ ] Requesting new ip adress')
print("[+] Changed IP to %s: " % self.get('https://ident.me').text) self.ctrl.signal(stem.Signal.NEWNYM)
time.sleep(self.ctrl.get_newnym_wait())
print("[+] Changed IP to %s: " % self.get(random.choice(self.ip_retrieval_sites)).text)
def close(self):
self.ctrl.close()
def reset(self): def reset(self):
self.new_identity() self.new_identity()
self.tr.session.cookies.clear() self.headers['UserAgent'] = UserAgent().random
self.tr.session.headers['UserAgent'] = UserAgent().random

Loading…
Cancel
Save