Lets say that I import a module using the requests library, but want to use a proxy with it. I have two options here: make a custom patch to this library so that I can use proxies or use a wrapper script similar to torify to achieve what I want.
Neither of these options are suitable for me, so I'm wondering if there's a way to use monkey-patching or a similar style to modify the requests library inside a module (inside a class) that I do not own.
EDIT:
@zedfoxus I am using the Mega.py library located at: richard.../mega.py
Here's the monkey-patching techniques I've tried so far:
import inspect
from mega import mega
pr = 'https://103.9.124.210:8080'
p = {'https': pr, 'http': pr}
string = (
inspect.getsource(mega).replace(
'from .', 'from mega.' # fix relative imports
).replace(
'requests', '_req'
).replace(
'import _req', f'import requests\n_req = requests.Session\n_req.proxies = {p}'
).replace(
'def login(self, email=None, password=None):', 'def login(self, email=None, password=None):\n print("milkGang")'
).replace(' ', '\t') # follow my indentation style (tabs)
)
# milkgang shows up when I run the call, but the request proxy is not set :(
del mega
exec(string)
m = Mega()
...
I have also tried:
import requests
requests = requests.Session() # yes I know this is dangerous
### paste proxy code from earlier here ###
requests.proxies = p
import mega
and
import mega
import requests
requests = requests.Session() # yes I know this is dangerous
### paste proxy code from earlier here ###
requests.proxies = p
as well as re-defining the mega.Mega
class
EDIT 2:
modifying the first attempt that I made to say Session()
instead of Session
seems to work, however, is there a cleaner method?