0

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?

255.tar.xz
  • 700
  • 7
  • 23
  • Is this what you are thinking of? https://stackoverflow.com/questions/30286293/make-requests-using-python-over-tor. This blog also might help: https://www.scrapehero.com/make-anonymous-requests-using-tor-python/ – zedfoxus May 31 '20 at 18:47
  • No, I'm trying to monkeypatch into another module to use a proxy, but cannot seem to modify it's version of the requests module. – 255.tar.xz May 31 '20 at 18:53
  • Could you share the other module and the changes you have tried? – zedfoxus May 31 '20 at 19:03
  • @zedfoxus I edited my question – 255.tar.xz May 31 '20 at 20:38
  • Use the method that works and optimize later. Right off, I can't think of a cleaner method than the one that works for you right now. – zedfoxus May 31 '20 at 20:41

1 Answers1

0

After revisiting this, it appears that the mega module has a self attribute named mega (mega.mega) in which I can see the imported modules (and due to pythons nature, also change them easily)

import mega
from requests import Session
mega.mega.requests = Session()
# :)
255.tar.xz
  • 700
  • 7
  • 23