def get_session(self): adapter = TorHttpAdapter(self._guard, self._hops_count) with Session() as s: s.headers.update(self._headers) s.mount('http://', adapter) s.mount('https://', adapter) yield s
def new(self): if self.use_tor: self._guard = self._tor.get_guard() adapter = TorHttpAdapter(self._guard, 3, retries=0) self.session = Session() self.session.mount('http://', adapter) self.session.mount('https://', adapter) else: self.session = Session()
def test_requests_session2(): tor = TorClient() with tor.get_guard() as guard: adapter = TorHttpAdapter(guard, 3, retries=RETRIES) with requests.Session() as s: s.headers.update({'User-Agent': 'Mozilla/5.0'}) s.mount('http://', adapter) s.mount('https://', adapter) #r = s.get('https://google.com', timeout=30) r = s.get('http://u7spnj3dmwumzoa4.onion', timeout=30) print(r) print(r.text[:1000])
def test_session(self): with self.tor.get_guard() as guard: adapter = TorHttpAdapter(guard, 3) with requests.Session() as s: s.headers.update({'User-Agent': self.agent}) s.mount('http://', adapter) s.mount('https://', adapter) r = s.get(self.host, timeout=30) self.logger.warning(r) self.logger.warning(r.text) assert r.text.rstrip().endswith('</html>') r = s.get(self.host) assert r.text.rstrip().endswith('</html>') self.logger.warning(r) self.logger.warning(r.text)
def test_adapter(): tor = TorClient() with tor.get_guard() as guard: adapter = TorHttpAdapter(guard, 3) with requests.Session() as s: s.headers.update({'User-Agent': 'Mozilla/5.0'}) s.mount('http://', adapter) s.mount('https://', adapter) r = s.get('https://google.com', timeout=30) logger.warning(r) logger.warning(r.text) assert "</body></html>" in r.text r = s.get('https://cryptowat.ch/assets/btc') logger.warning(r) logger.warning(r.text)
def test_requests_session(): tor = TorClient() with tor.get_guard() as guard: adapter = TorHttpAdapter(guard, 3, retries=RETRIES) with requests.Session() as s: s.headers.update({'User-Agent': 'Mozilla/5.0'}) s.mount('http://', adapter) s.mount('https://', adapter) r = s.get('https://google.com', timeout=30) logger.warning(r) logger.warning(r.text) assert r.text.rstrip().endswith('</html>') r = s.get('https://stackoverflow.com/questions/tagged/python') assert r.text.rstrip().endswith('</html>') logger.warning(r) logger.warning(r.text)
from torpy import TorClient from torpy.utils import recv_all from torpy.http import requests from torpy.http.adapter import TorHttpAdapter host = "ifconfig.me" with TorClient() as tor: with tor.create_circuit(3) as circ: with circ.create_stream((host, 80)) as stream: stream.send(b'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % host.encode()) ret = recv_all(stream).decode() print(ret) host2 = 'https://www.facebookcorewwwi.onion/' with TorClient() as tor: with tor.get_guard() as guard: adapter = TorHttpAdapter(guard, 3) with requests.Session() as sess: sess.headers.update({'User-Agent': 'Mozilla/5.0'}) sess.mount('http://', adapter) sess.mount('https://', adapter) resp = sess.get("http://" + host, timeout=15) print(resp.text) resp = sess.get(host2) print(resp.text)