vgrem / Office365-REST-Python-Client

Microsoft 365 & Microsoft Graph Library for Python
MIT License
1.29k stars 332 forks source link

Request with proxy - is it possible? #382

Open wennaspeedy opened 3 years ago

wennaspeedy commented 3 years ago

I'm trying to upload a file to sharepoint via proxy. I read that it should work via os.environ['http_proxy'], but it doesn't work. I'm getting an SSL error. Is there a way to add a proxy address as a request parameter?

wennaspeedy commented 3 years ago

I've already done it. But so far I've rewritten the hardcoded requests - I've set the proxy and verification to false. Maybe sometime I'll do it in general and make a commit.

wennaspeedy commented 3 years ago

Maybe it would be enough to os.environ['http_proxy'] and turn off verifications. Can it be done (verification in request) with some method in this module???

vgrem commented 3 years ago

How about passing proxies argument as demonstrated below?

from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.client_context import ClientContext

def set_proxy(request):
    proxies = {site_url: 'https://127.0.0.1:8888'}
    request.proxies = proxies

ctx = ClientContext(site_url).with_credentials(ClientCredential(client_id, client_secret))

ctx.pending_request().beforeExecute += set_proxy

target_web = ctx.web.get().execute_query()
wennaspeedy commented 3 years ago

This workaround is not working. I need turn off the verification for my proxy server.

I had to add 'verify= False, proxies=proxies' to all requests calling.

F.E.:

      proxies = { 
    'http': 'http://myproxy:port',
    'https': 'http://myproxy:port',
}
        resp = requests.post(self._sts_profile.user_realm_service_url, data="login={0}&xml=1".format(self._username),
                             headers={'Content-Type': 'application/x-www-form-urlencoded'}, verify= False, proxies=proxies)
chan0923 commented 1 year ago

sharing my workaround that works on my end:

  1. set proxy at global envrionemt using os
  2. create a context to apply "verify = false" for all requests within the context (taken reference from https://stackoverflow.com/questions/15445981/how-do-i-disable-the-security-certificate-check-in-python-requests)
  3. wrap the codes inside the context
import os
import requests
import warnings
import contextlib
import requests
from urllib3.exceptions import InsecureRequestWarning

# Set global proxy settings
proxy = 'http://<ip>:<port>'
os.environ['http_proxy'] = proxy 
os.environ['HTTP_PROXY'] = proxy
os.environ['https_proxy'] = proxy
os.environ['HTTPS_PROXY'] = proxy

# Set a context to force no SSL verification
old_merge_environment_settings = requests.Session.merge_environment_settings

@contextlib.contextmanager
def no_ssl_verification():
    opened_adapters = set()

    def merge_environment_settings(self, url, proxies, stream, verify, cert):
        # Verification happens only once per connection so we need to close
        # all the opened adapters once we're done. Otherwise, the effects of
        # verify=False persist beyond the end of this context manager.
        opened_adapters.add(self.get_adapter(url))

        settings = old_merge_environment_settings(self, url, proxies, stream, verify, cert)
        settings['verify'] = False

        return settings

    requests.Session.merge_environment_settings = merge_environment_settings

    try:
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', InsecureRequestWarning)
            yield
    finally:
        requests.Session.merge_environment_settings = old_merge_environment_settings

        for adapter in opened_adapters:
            try:
                adapter.close()
            except:
                pass

# wrote all code within the context
with no_ssl_verification():
    site_url = "https://somesite.sharepoint.com/sites/somepage"
    ctx = ClientContext(site_url).with_credentials(UserCredential(username, pswd))

    web = ctx.web
    ctx.load(web)
    ctx.execute_query()
    print("Web title: {0}".format(web.properties['Title']))
chan0923 commented 1 year ago

Agree with @wennaspeedy that using the "beforeExecute" method doesn't seems to modify the behavior at all. Lots of workaround needed to alter the behavior of the underlying requests object, mainly to 1) set proxy and 2) disable SSL verification.

hope this gets addressed and included in the default behavior while using the library.