Closed ProtDos closed 8 months ago
from torpy.http.requests import TorRequests
# This doesn't work
req = TorRequests()
sess = req.get_session()
r = sess.get("http://example.org")
print(r.text)
exit()
""" # This works
with TorRequests() as tor_requests:
print(tor_requests)
print("building circuit...")
with tor_requests.get_session() as session:
response = session.get("http://httpbin.org/ip").json()
print(response)
ip_address = response["origin"]
print(f"Your IP address is: {ip_address}")
"""
The code above is what I have tried (using it without with
), but it raised this error:
Traceback (most recent call last):
File "D:\...\...\v2.py", line 6, in <module>
r = sess.get("http://example.org")
AttributeError: '_GeneratorContextManager' object has no attribute 'get'
Fixed by:
`from requests import Session from torpy.http.adapter import TorHttpAdapter from torpy.http.requests import TorRequests
req = TorRequests() req.enter()
adapter = TorHttpAdapter(req._guard, req._hops_count, retries=0) s = Session() s.headers.update(req._headers) s.mount('http://', adapter) s.mount('https://', adapter)
r = s.get("http://httpbin.org/ip") print(r.text)`
Hello, my problem is the fact, that I want to use the same session to use in other function of my script. I am working on an app that allows users to send a simple tor request to a given site. I want to use the same session for new requests, generated in the init function. But I always seem to get errors. This is the current code: `import traceback
import requests from kivy import platform from torpy.http.requests import TorRequests from kivy.app import App from kivy.uix.button import Button from kivy.uix.label import Label from kivy.uix.boxlayout import BoxLayout import threading from kivy.clock import mainthread import urllib3 if platform == "android": from android.permissions import request_permissions, Permission
class MyApp(App): session = None tor_requests = None
if name == 'main': MyApp().run() `
I tried using this in
test_session()
, but it simply created a new session.with self.tor_requests as r: with r.get_session() as s: print(s) response = s.get("http://httpbin.org/ip").json()
How can I fix it? Or are there better ways to do it?